我在 objc io 书中看到过这样的示例
callback(Result {
if let e = error {
throw e
}
guard let d = data else {
throw NoDataError()
}
return try JSONDecoder().decode(User.self, from: d)
})
回调在哪里
“callback: @escaping (Result<User, Error>) -> ()
似乎 Result(Swift 定义的枚举类型)正在使用闭包来创建自身?
但我没有看到任何相关文档
最佳答案
有文档。
该表达式表示 init(catching:) 的尾随闭包语法方法Result 。说明是
Creates a new result by evaluating a throwing closure, capturing the returned value as a success, or any thrown error as a failure.
完整的语法是
callback(Result(catching: {
if let e = error {
throw e
}
guard let d = data else {
throw NoDataError()
}
return try JSONDecoder().decode(User.self, from: d)
})
)
关于ios - 作为构造函数关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56608458/