有没有办法从惰性或计算属性调用异步函数?
struct Item {
lazy var name: String = {
API.requestThing({ (string: String) in // Xcode didn't like this
return string // this would not work here
})
}()
}
class API {
class func requestThing(completion: String -> Void) {
completion("string")
}
}
最佳答案
API.requestThing 中的完成处理程序返回一个字符串,但它应该没有返回值:
(completion: String -> Void)
我让这个工作:
struct Item {
lazy var name: String = {
API.requestThing({ (string: String) in
return string
})
}()
}
class API {
class func requestThing(completion: String -> String) -> String {
return completion("string")
}
}
关于ios - 在惰性 var 属性中调用异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379619/