【发布时间】:2016-01-25 03:57:30
【问题描述】:
我有一个循环,旨在使用 Alamofire 对 Youtube 的 API 进行 10 次不同的调用,每个调用都有一个不同的 videoID,我从另一个函数中获得。但是,似乎没有进行调用,因为我没有从 Alamofire 开关中的“成功”或“失败”选项中获得结果。
谁能指出问题所在?
func getRecom(completion: (result:String)->()) {
let array = defaults.arrayForKey("recomQuery")
for i in 0..<10 {
let videoURL = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet&id=\(array![i])&maxResults=10&key=\(apiKey)"
Alamofire.request(.GET, videoURL).validate().responseJSON { response in
switch response.result {
case .Success:
print("success") //THIS IS NOT BEING CALLED
if let value = response.result.value {
let json = JSON(value)
let videoTitle = json["items"][0]["snippet"]["title"].stringValue
print("Title from network \(videoTitle)")
let thumbPath = String(json["items"][0]["snippet"]["thumbnails"]["default"]["url"])
print(i)
let image = UIImage(data: NSData(contentsOfURL: NSURL(string: thumbPath)!)!)!
let newImage: NSData = UIImagePNGRepresentation(image)!
self.recomTitles.append(videoTitle)
self.recomThumbs.append(newImage)
}
case .Failure(let error):
print("failure") //THIS IS ALSO NOT BEING CALLED
print(error)
completion(result: "done")
}
}
}
print("saving array: \(defaults.arrayForKey("recomTitles"))")
defaults.setObject(recomThumbs, forKey: "recomThumbs")
defaults.setObject(recomTitles, forKey: "recomTitles")
completion(result: "done")
}
提前致谢!
【问题讨论】:
标签: swift loops youtube-api alamofire swifty-json