&#39;(_,_,_) - &gt;虚空&#39;不能转换为&#39;结果<post> - &gt;虚空&#39;

时间:2015-10-10 05:12:02

标签: ios swift alamofire

在视图控制器中,我有以下代码:

newPost!.save({ (post, error) in
        //Error here. 

        if let anError = error {
            print("error calling POST on /posts")
            print(anError)
            return
        }
        guard let post = post else {
            print("error calling POST on /posts: result is nil")
            return
        }
        // success!
        print(post.description())
        print(post.title)

    })

我不太了解Result<Whatever>语法,Result应该来自Alamofire 2,Post是我创建的类,但Result<Post> -> Void是什么完全意味着我应该改变什么呢?

更新:

func save(completionHandler: (Result<Post>) -> Void) {

let fields: [String: AnyObject]? = self.toJSON()
if fields == nil {

print("Error: error converting newPost fields to JSON")
return

}

1 个答案:

答案 0 :(得分:0)

你的完成处理程序定义和你调用它的方式是不同的,如果你想以这种方式调用它,请尝试使用这样的完成处理程序:

func save(completionHandler: (post:Post?, error:String?) -> Void) {

    let fields: [String: AnyObject]? = self.toJSON()
    if fields == nil {
       completionHandler(nil, "Error converting newPost fields to JSON")
       return
    }
    // rest of code, eventually call completionHandler with success
}

对于Swift 2:

func save(completionHandler: (post:Post?, error:String?) -> Void) {

    guard let fields = self.toJSON() else {
         completionHandler(nil, "Error converting newPost fields to JSON")
         return
    }
    // rest of code, eventually call completionHandler with success

}

Closures Documentation