gpt4 book ai didi

swift - 使用 Swift 获取核心数据实体

转载 作者:行者123 更新时间:2023-11-28 15:14:34 24 4
gpt4 key购买 nike

整个下午我都在为这个问题苦苦挣扎。我可以在睡梦中在 Obj-C 中执行此操作,但我似乎无法理解我应该在 Swift 中执行的操作。我可以取回一组实体 (StoredTopic),但我在尝试访问它们时不断出错。

获取代码如下:

func fetchCoreData(_ entityName:String) -> [NSManagedObject] {

guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return []
}

let managedContext =
appDelegate.persistentContainer.viewContext

let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: entityName)

do {

let arrData = try managedContext.fetch(fetchRequest)


} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}

return []
}

这是arrData的日志

[<StoredTopic: 0x6040000833e0> (entity: StoredTopic; id: 0xd000000000040000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p1> ; data: <fault>), <StoredTopic: 0x604000085960> (entity: StoredTopic; id: 0xd000000000080000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p2> ; data: <fault>), <StoredTopic: 0x604000083020> (entity: StoredTopic; id: 0xd0000000000c0000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p3> ; data: <fault>), <StoredTopic: 0x604000088de0> (entity: StoredTopic; id: 0xd000000000100000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p4> ; data: <fault>), <StoredTopic: 0x60000008c580> (entity: StoredTopic; id: 0xd000000000140000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p5> ; data: {
createDt = "2017-11-02 19:28:00 +0000";
segObj = 0;
segTopic = 0;
title = Test;
topicID = "4A55582B-3084-4ECA-8F61-C1193A85B45B";
"topic_desc" = 1234567890;
updateDt = "2017-11-02 19:28:00 +0000";

所以如果我尝试做类似的事情:

arrData[0].topicID 我得到“‘NSManagedObject’类型的值没有成员‘topicID’”

如果我尝试通过添加此行建议的其他一些 SO 帖子来做:

var arrStoredTopic = [StoredTopic]()
arrStoredTopic = try managedContext.fetch(fetchRequest)

我收到“无法将类型‘[NSManagedObject]’的值分配给类型‘[StoredTopic]’”

然后我尝试按照本教程进行操作,尽管其中一些调用有点过时并更改了我的代码以进行操作:

func fetchCoreData(_ entityName:String) -> [NSManagedObject] {

guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return []
}

let managedContext = appDelegate.persistentContainer.viewContext
var fetchRequest = NSFetchRequest<NSFetchRequestResult>()
let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: managedContext)
fetchRequest.entity = entityDescription

do {

//let arrData = try managedContext.fetch(fetchRequest)
let results = try managedContext.fetch(fetchRequest)

if results.count > 0 {

let storedData = results[0] as! NSManagedObject

}

} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}

return []
}

仍然无法访问 StoredTopic 属性。

那么我如何从 Core Data 中获取 managedObjects 以便我可以使用它们呢?我显然很困惑。

谢谢

最佳答案

改变

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entityName)

let fetchRequest = NSFetchRequest<StoredTopic>(entityName: entityName)

let fetchRequest = StoredTopic.fetchRequest()

在您的原始代码中,您说获取请求将获取 NSManagedObject 而不是 StoredTopic,这就是为什么您在访问属性和分配结果时遇到问题对 [StoredTopic] 的获取请求。

Swift 是一种类型安全的语言,因此它对 Objective-C 不担心的事情非常严格(直到它在运行时崩溃)。

关于swift - 使用 Swift 获取核心数据实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083482/

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