所以我有一个问题,我把一些项目放在一个对象后调用save。我去了一个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()
}
答案 0 :(得分:2)
问题是您要将两个值始终保存在同一个PFObject
实例中。
在循环中移动行let spots = PFObject(className:"spot")
。
PS:使用Swift原生类型。例如,这是更有效的
let location = geometry["location"] as! [String:Double]
let cLat = location["lat"]!
let cLon = location["lng"]!