gpt4 book ai didi

ios - CoreData 获取返回 nil

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

为什么当我在第一个位置打印 myData 数组时,我所有的 valueForKey 都为 nil,就像这样

  func fetchFavorites(){

let fetchRequest = NSFetchRequest(entityName: "Favoritos")

do {
let result = try moc.executeFetchRequest(fetchRequest)
for managedObject in result {

if let Fid = managedObject.valueForKey("place_favorite_id"),
desc_place = managedObject.valueForKey("desc_place"),
place_name = managedObject.valueForKey("place_name"),
lat = managedObject.valueForKey("latitude"),
lon = managedObject.valueForKey("longitude") {

print("\(Fid) \(desc_place) \(place_name) \(lat) \(lon)")
Place_Id.append(Fid as! String)

}

}
myData = result

} catch {
let fetchError = error as NSError
print(fetchError)
}
}

打印的 myData 数组

    Array[<RivosTaxi.Favoritos: 0x7fb46ff5ac80> (entity: Favoritos; id: 0xd00000000024000e <x-coredata://82CCF746-275D-4FC6-9C5C-EFD1EDED2F21/Favoritos/p9> ; data: {
"desc_place" = nil;
latitude = nil;
longitude = nil;
"place_favorite_id" = nil;
"place_name" = nil;

}), <RivosTaxi.Favoritos: 0x7fb46ff5af80> (entity: Favoritos; id: 0xd00000000028000e <x-coredata://82CCF746-275D-4FC6-9C5C-EFD1EDED2F21/Favoritos/p10> ; data: {
"desc_place" = "";
latitude = "24.822311";
longitude = "-107.4240634";
"place_favorite_id" = 94;
"place_name" = "Otro mad";

那是我不明白的,第一个值都是零,我的 sqlite 中只有一行数据,而不是两行

这就是你将它保存到 Favoritos 实体的方式

    func FavoritosSave(){

let entity = NSEntityDescription.insertNewObjectForEntityForName("Favoritos", inManagedObjectContext: moc)



entity.setValue(place_name, forKey: "place_name")
entity.setValue(place_favorite, forKey: "place_favorite_id")
entity.setValue(desc_place, forKey: "desc_place")
entity.setValue(latitude, forKey: "latitude")
entity.setValue(longitude, forKey: "longitude")

// we save our entity
do {
try moc.save()


} catch {
fatalError("Failure to save context: \(error)")
}
}

最佳答案

您的提取请求被写入以返回实体名称为“Favoritos”的所有实例。该搜索结果显示您已经创建了两个。

只看到一行的解释可能是您的 MOC 尚未保存。但是在 Core Data 的 SQLite 文件中翻找垃圾箱会让人感到沮丧。使用记录的 API。

您应该在 save() 函数的第一行添加对 fetchFavoritos() 的调用,以便在创建 Favoritos 实例之前查看那里有什么。

关于ios - CoreData 获取返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35491299/

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