我想弄清楚如何从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