gpt4 book ai didi

ios - “(_, _, _) -> Void”无法转换为 'Result -> Void'

转载 作者:行者123 更新时间:2023-11-30 14:00:32 30 4
gpt4 key购买 nike

在 View Controller 中我有以下代码:

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

}

最佳答案

您的完成处理程序定义和调用它的方式不同,如果您想这样调用它,请为您的完成处理程序尝试类似的方法:

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

关于ios - “(_, _, _) -> Void”无法转换为 'Result<Post> -> Void',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050548/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com