Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将objective-c块转换为快速闭合_Objective C_Swift - Fatal编程技术网

将objective-c块转换为快速闭合

将objective-c块转换为快速闭合,objective-c,swift,Objective C,Swift,我正在尝试将Objective-C块转换为Swift: [self.client downloadEntity:@"Students" withParams: nil success:^(id response) { // execute code } failure:^(NSError *error) { // Execute code }]; 这是我在Swift中的代码,但语法似乎有点错误: client.downloadEntity("Students",

我正在尝试将Objective-C块转换为Swift:

[self.client downloadEntity:@"Students" withParams: nil success:^(id response) {
      // execute code
}
failure:^(NSError *error) {
    // Execute code

}];
这是我在Swift中的代码,但语法似乎有点错误:

    client.downloadEntity("Students", withParams: nil, success:  {(students: [AnyObject]!) -> Void in
        print("here")
        },  failure:  { (error: NSError!) -> Void! in
            print ("here")
     }
这给了我一些编译错误:

  • “AnyObject”的值没有成员“downloadEntity”
  • 它抱怨代码失败部分之后缺少逗号(,)

  • 您需要切换到新的Swift错误语法,还可以使用尾部闭包。我不得不以bool为例来说明如何称成功为闭包,或者抛出错误

    var wasSuccessful = true // This is just here so this compiles and runs
    
    // This is a custom error type. If you are using something that throws an
    // NSError, you don't need this.
    enum Error:ErrorType {
        case DownloadFailed
    }
    
    // Hopefully you have control over this method and you can update
    // the signature and body to something similar to this:
    func downloadEntity(entityName: String, success: ([AnyObject]) -> Void)  throws {
        let students = [AnyObject]()
    
        // download your entity
        if wasSuccessful {
            // Call your success completion handler
            success(students)
        }
        else {
            throw Error.DownloadFailed
        }
    }
    
    当您有一个可以抛出错误的函数时,您需要在do/catch块中使用try调用它

    // Calling a function that can throw
    do {
        try downloadEntity("Students") { students in 
            print("Download Succeded")
        }
    }
    catch Error.DownloadFailed {
        print("Download Failed")
    }
    // If you are handling NSError use this block instead of the one above
    // catch let error as NSError {
    //     print(error.description)
    // }
    
    试试这个:

    client.downloadEntity("Student", withParams: nil,
            success: { (responseObj) -> Void in
                print("success: \(responseObj)")
            },
    
            failure: { (errorObj) -> Void in
                print("treat here (in this block) the error! error:\(errorObj)")
            })
    

    你凭什么认为代码有问题?它会给你编译错误吗?它是否给出运行时错误?是的。编译错误。我在上面添加了它们。客户端没有成员“downloadEntity”,这可能就是问题所在。来自#1的错误与逗号无关,clang抱怨某个对象没有
    downloadEntity
    成员。
    client
    变量的数据类型是什么?这个答案是一个很好的开始,是否有任何方法可以解释您的解决方案是如何解决问题的?只有语法错误。Xcode自动完成功能将帮助您使用正确的Swift等效方法。有什么东西吗?应使用对象名称更改。这就是全部