【问题标题】:Fetching data using relationships in coreData swift使用 coreData swift 中的关系获取数据
【发布时间】:2016-06-16 10:30:09
【问题描述】:

CoreData Model

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

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

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

【问题讨论】:

  • 你创建了实体类吗?

标签: ios swift core-data relationships


【解决方案1】:

您需要创建一个 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
}

但它很丑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多