gpt4 book ai didi

ios - 嵌套函数上的两个循环导致索引超出范围

转载 作者:行者123 更新时间:2023-11-30 13:45:40 25 4
gpt4 key购买 nike

我有一个调用 Parse.com 的函数,返回一个包含 10 个 ID 的数组(带有一个循环),然后对于每个 ID,它都会在 youtube 的 API 中查找其数据(同样带有一个循环)。

我收到“索引越界”错误,因为由于某种原因,索引不会在第二个循环中重新开始,而是从第一个循环结束时继续。有人可以指出我可能做错了什么吗?

func recomendedQuery(completion: (result: String)->()) {
let query = PFQuery(className: "VideosInfo")
query.whereKeyExists("Rating")
query.limit = 10
query.orderByDescending("Rating")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
for var i=0; i<10; i++ {
let id = objects![i]["Video_ID"] as! String
print(id) //No problems here
self.recomQuery.append(id)
}
defaults.setObject(self.recomQuery, forKey: "recomQuery")
} else {
print(error?.description)
}
completion(result:"done")
}
}

func getRecom(completion: (result:String)->()) {
let array = defaults.arrayForKey("recomQuery")
print(array![0])

for var i=0; i<10; i++ {
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:
if let value = response.result.value {
let json = JSON(value)
let videoTitle = json["items"][i]["snippet"]["title"].stringValue
let thumbPath = String(json["items"][i]["snippet"]["thumbnails"]["default"]["url"])
print(videoTitle)
print(thumbPath)
print(i) //ERROR: PRINTS 10 AND ITS OUT OF BOUNDS
let image = UIImage(data: NSData(contentsOfURL: NSURL(string: thumbPath)!)!)!
let newImage: NSData = UIImagePNGRepresentation(image)!
self.recomTitles.append(videoTitle)
self.recomThumbs.append(newImage)
defaults.setBool(true, forKey: "gotRecom")
completion(result: "done")
}
case .Failure(let error):
print(error)
completion(result: "done")
}
}
}
defaults.setObject(recomThumbs, forKey: "recomThumbs")
defaults.setObject(recomTitles, forKey: "recomTitles")
}

recomendedQuery { (result:String) -> () in //Call functions
//CHANGE TO ONLY IF SUCCESS
Networking().getRecom() { (result:String) -> () in
self.recomView.reloadData()
}
}

最佳答案

尝试这样。这可能对您有帮助。

func getRecom(completion: (result:String)->()) {

let array = defaults.arrayForKey("recomQuery")
print(array![0])

for (index, object) in enumerate(array) {

let videoURL = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet&id=\(object)&maxResults=10&key=\(apiKey)"

Alamofire.request(.GET, videoURL).validate().responseJSON { response in
switch response.result {

case .Success:
if let value = response.result.value {
let json = JSON(value)
let videoTitle = json["items"][index]["snippet"]["title"].stringValue
let thumbPath = String(json["items"][index]["snippet"]["thumbnails"]["default"]["url"])
print(videoTitle)
print(thumbPath)
print(index)
let image = UIImage(data: NSData(contentsOfURL: NSURL(string: thumbPath)!)!)!
let newImage: NSData = UIImagePNGRepresentation(image)!
self.recomTitles.append(videoTitle)
self.recomThumbs.append(newImage)
defaults.setBool(true, forKey: "gotRecom")
completion(result: "done")
}
case .Failure(let error):
print(error)
completion(result: "done")
}
}
}

defaults.setObject(recomThumbs, forKey: "recomThumbs")
defaults.setObject(recomTitles, forKey: "recomTitles")
}

recomendedQuery { (result:String) -> () in //Call functions
//CHANGE TO ONLY IF SUCCESS
Networking().getRecom() { (result:String) -> () in
self.recomView.reloadData()
}
}

关于ios - 嵌套函数上的两个循环导致索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com