如何在Swift中将completionHandler Closure与Return结合使用?

如何在Swift中将completionHandler Closure与Return结合使用?,第1张

如何在Swift中将completionHandler Closure与Return结合使用?
func getSomething(callback: (Array<AnyObject>) -> ()) {    var dataTask = NSURLSessionDataTask()    dataTask = session.dataTaskWithRequest(request) { (data, response, error) in        if (error == nil) { var callbackArray = Array<MyObject>() let responseDict = NSJSONSerialization.JSonObjectWithData(data, options: .MutableContainers, error: nil) as NSDictionary let response = responseDict.objectForKey("response_key") as NSDictionary let array = response.objectForKey("array_key") as NSArray for item: AnyObject in array {     var arrayItem = MyObject(dict: item as NSDictionary)     callbackArray.append(arrayItem) } callback(callbackArray)        } else { // handle an error        }    }    dataTask.resume()}

然后,您可以执行以下 *** 作

getSomething() { (response) in    if let responseArray = response as? Array<MyObject> {        self.somethings = responseArray    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4913309.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存