[英]Fetching data using relationships in coreData swift
let newUser = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext:context) as NSManagedObject
Above CoreData Model image contains my data model. 上面的CoreData Model图像包含我的数据模型。
I am not able to access time using newUser ie; 我无法使用newUser即访问时间; newUser.time is giving me an error "Value of type 'NSManagedObject' has no member 'time'" newUser.time给我一个错误“类型为'NSManagedObject'的值没有成员'time'”
You need to create an NSManagedObject subclass (in Editor menu or manually). 您需要创建一个NSManagedObject子类(在“编辑器”菜单中或手动创建)。 Then, you can use the following code (Employee being your subclass) 然后,您可以使用以下代码(Employee是您的子类)
let entityDesc = NSEntityDescription.entityForName("Employee", inManagedObjectContext: managedContext)!
let entity = Employee(entity: entityDesc, insertIntoManagedObjectContext : managedContext)
To access all your fields. 要访问您的所有字段。 Creating a subclass is not mandatory though, you can always use your NSManagedObject as a dictionary and do 创建子类不是强制性的,但是您始终可以将NSManagedObject用作字典并执行
if let time = newUser.valueForKey("time") as? NSDate {
//use date here
}
However it is ugly. 但是这很丑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.