我有一个方法,我想在 Alamofire 回调成功时返回结构数组。
func getPopularMedias() -> BFTask {
let instaUrl: String = "https://api.instagram.com/v1/media/popular"
let user: PFUser = currentUser()
let accessToken = user.objectForKey("accessToken") as! String
var medias: [Media] = []
let task = BFTaskCompletionSource()
Alamofire.request(.GET, instaUrl, parameters: ["access_token": accessToken])
.responseJSON { request, response, data in
var json = JSON(data.value!)
for(_, subJson): (String, JSON) in json["data"] {
let image: UIImage = NSURL(string: subJson["images"]["low_resolution"]["url"].stringValue)
.flatMap { NSData(contentsOfURL: $0) }
.flatMap { UIImage(data: $0) }!
let profileImage: UIImage = NSURL(string: subJson["user"]["profile_picture"].stringValue)
.flatMap { NSData(contentsOfURL: $0) }
.flatMap { UIImage(data: $0) }!
medias.append(Media(name: subJson["user"]["full_name"].stringValue, image: image, profileImage: profileImage))
}
task.setResult(medias)
}
return task.task
}
设置时task.setResult
我收到一个错误,称为cannot convert value of [Media] to expected argument AnyObject!
目前我在 XCode 7.0 GM 版本和 swift 2 上运行它。