在视图控制器中,我有以下代码:
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
}
答案 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
}