【发布时间】:2016-04-02 21:30:13
【问题描述】:
我正在尝试做某事可能很奇怪,但我遇到了以下问题。
我有一个带有闭包属性的结构,我设置了异步函数。
我想要的是,调用这个异步函数来获取返回值。
我可以通过变通解决方案来解决这个问题,但我想用正确的方法来解决这个问题。
感谢任何帮助。
这是我的代码:
struct Item {
var myselector: (String -> Void)?
func getMeThat(completion: String -> Void) {
completion(myselector()) // error: cannot call value of non-function type String -> Void
}
}
class API {
class func requestThing(completion: String -> Void) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
completion("Kenan")
})
}
}
class ViewController {
func viewDidLoad() {
var item = Item()
item.myselector = { // error: cannot assign value of type () -> () to type String -> Void
API.requestThing({ (str: String) in
<#code#>
})
}
item.getMeThat({ (rtn: String) in
print(rtn)
})
}
}
【问题讨论】:
-
您能否将错误消息直接编辑到您的问题中而不是屏幕截图中?
标签: ios swift asynchronous closures