1
オブジェクトにいくつかのアイテムを入れた後にsaveを呼び出すという問題があります。私はAPIに行くと私はいくつかの情報をダウンロードし、私は一時的な使用のために別のシステムに保存するが.save()は、それが選択する特別なパターンなしで2つの項目を保存するようだ。誰かが問題の内容を説明することはできますか?whileループでパースするために保存するswift
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()
}
「HotSpots」とは何ですか?サイドノート:「while i <...」とカウンタをインクリメントすることは、Swiftの構文が非常に悪いことです。通常の構文は 'for-in'です。 – vadian
私はそれを修正することを確認します!おかげで –