我想弄清楚如何從JSON
中存儲CoreData
中的位置。 我使用此代碼來從JSON
API數據,並試圖將其存儲在我的CoreData
CoreData中的商店位置(Swift)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newPok = NSEntityDescription.insertNewObject(forEntityName: "Person", into: context)
for jsonItem in json {
let item = jsonItem as! NSDictionary
let pokemonName = item["name"]
let pokemonDesc = item["description"]
let pokemonLat = (item["location"] as! NSDictionary) ["latitude"]
let pokemonLon = (item["location"] as! NSDictionary) ["longitude"]
let coordinates = CLLocationCoordinate2D(latitude: pokemonLat as! CLLocationDegrees, longitude: pokemonLon as! CLLocationDegrees)
let annotation = MKPointAnnotation()
annotation.title = pokemonName as! String?
annotation.subtitle = pokemonDesc as! String?
annotation.coordinate = coordinates
self.map.addAnnotation(annotation)
newPok.setValue(pokemonName as! String, forKey: "name")
newPok.setValue(pokemonDesc as! String, forKey: "desc")
newPok.setValue(pokemonLat as! String, forKey: "lat")
newPok.setValue(pokemonLon as! String, forKey: "lon")
do {
try context.save()
} catch {
print("not saved")
}
}
但是當我運行這段代碼,我總是得到一個fatal exception
。
error Domain=NSCocoaErrorDomain Code=134190 "(null)" UserInfo={entity=Person, property=lat, reason=Source and destination attribute types are incompatible}
我的數據庫中有一個設置有4個字符串(名稱,說明,緯度,經度)
有誰知道我在做什麼錯在這裏?
親切的問候,
約翰
core-data中'lat'的類型? –
類型是字符串。但我認爲這是不正確的 – Johan
雖然縮進...... o.0 – Fogmeister