无法将回调函数中的值存储到类变量中:Swift

时间:2015-12-03 05:51:07

标签: swift asynchronous callback

我有一个回调函数,可以从url中检索内容,但是信息不会存储到类变量中。

var counts:Int?
var imagesComments : [NSArray] = []

loader.getFeed("1", completionHandler: { response in
        if let dataArr = response["content"] as? NSArray{
            for downloaded_images in dataArr{
                if let image = downloaded_images as? NSDictionary{
                    let url = image["url"] as? String
                    // Get images and load into images
                    if let comments = image["comments"] as? NSArray{
                        dispatch_async(dispatch_get_main_queue()) {
                            self.imagesComments.append(comments)
                        }
                    }
                    self.loader.downloadImage(NSURL(string: self.baseUrl+url!)!, completionHandler: { response in
                        dispatch_async(dispatch_get_main_queue()) {
                            self.images.append(response)
                            self.pileView?.reloadContent()
                        }
                    })
                }
            }
        }
    })

运行此代码后,我有一个打印imagesComments和计数的地方,两者都是空的/无

JSON文件:

{
"success": true,
"message": null,
"content": [{
    "url": "6\/image_2.png",
    "date_added": "2015-12-02 22:43:05",
    "comments": ["Awesome Pic", "WOOHOOOOO THIS IS GREAT"],
    "likes": []
}, {
    "url": "2\/image_5.png",
    "date_added": "2015-12-01 06:43:48",
    "comments": ["EhHHHH"],
    "likes": []
}]
}

0 个答案:

没有答案