gpt4 book ai didi

ios - 使用 coreData 中的关系快速获取数据

转载 作者:行者123 更新时间:2023-11-30 13:20:04 26 4
gpt4 key购买 nike

CoreData Model

let newUser = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext:context) as NSManagedObject

上面的 CoreData 模型图像包含我的数据模型。

我无法使用 newUser 访问时间,即; newUser.time 给我一个错误“‘NSManagedObject’类型的值没有成员‘time’”

最佳答案

您需要创建一个 NSManagedObject 子类(在编辑器菜单中或手动)。然后,您可以使用以下代码(Employee是您的子类)

let entityDesc =  NSEntityDescription.entityForName("Employee", inManagedObjectContext: managedContext)!

let entity = Employee(entity: entityDesc, insertIntoManagedObjectContext : managedContext)

访问您的所有字段。创建子类并不是强制性的,您始终可以使用 NSManagedObject 作为字典并执行

if let time = newUser.valueForKey("time") as? NSDate {
//use date here
}

但是它很丑。

关于ios - 使用 coreData 中的关系快速获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856590/

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