gpt4 book ai didi

ios - 在 while 循环中快速保存到 Parse

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:35 24 4
gpt4 key购买 nike

所以我在将一些项目放入对象后调用保存时遇到了问题。我转到 API 并下载了一些信息,然后将其保存到另一个系统以供临时使用,但是 .save() 似乎只保存了 2 个项目,没有选择的特殊模式。谁能解释一下问题出在哪里?

let url = URL(string: link)
let spots = PFObject(className:"spot")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in

if error != nil {
print(error)
}
else{
if let urlContent = data{

do{
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
let results = (jsonResult as! NSDictionary)["results"]
let venueList = (results as! NSArray)
//print(jsonResult)
var i = 0
while i < venueList.count{
let venues = venueList[i] as! NSDictionary
let name = venues["name"] as! String
let geometry = venues["geometry"] as! NSDictionary
let location = geometry["location"] as! NSDictionary
let cLat = location["lat"] as! Double
let cLon = location["lng"] as! Double
let vPoint = PFGeoPoint(latitude: cLat, longitude: cLon)
//print(name," ", vPoint)
spots["venue"] = name
spots["name"] = vPoint
do{
try HotSpots.save()
print("Saved! ",name," ", vPoint)
}
catch{
print("Save Error")
}
i+=1
}




}
catch{
print("JSON Error")
}
}
}
}
task.resume()
}

最佳答案

问题是您总是将这两个值保存在同一个 PFObject 实例中。

在循环中移动 let spots = PFObject(className:"spot") 行。

PS:使用 Swift 原生类型。例如这样效率更高

let location = geometry["location"] as! [String:Double]
let cLat = location["lat"]!
let cLon = location["lng"]!

关于ios - 在 while 循环中快速保存到 Parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39320591/

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