Ios 为核心数据实体添加方便的初始值设定项
在Swift 3/Xcode 8中,如何为核心数据实体创建方便的初始值设定项?现在我有一个普通类,并使用一个方便的初始值设定项从JSON保存数据,如下所示:Ios 为核心数据实体添加方便的初始值设定项,ios,core-data,swift3,Ios,Core Data,Swift3,在Swift 3/Xcode 8中,如何为核心数据实体创建方便的初始值设定项?现在我有一个普通类,并使用一个方便的初始值设定项从JSON保存数据,如下所示: convenience init?(json: [String: Any]) { let id = json["id"] as! Int let title = json["title"] as! String let subtitle = json["subtitle"] as? String self.
convenience init?(json: [String: Any]) {
let id = json["id"] as! Int
let title = json["title"] as! String
let subtitle = json["subtitle"] as? String
self.init(
anId: id,
aTitle: title,
aSubtitle: subtitle,
)
}
class Foo: NSManagedObject {
@NSManaged var title: String
@NSManaged var subtitle: String?
@NSManaged var id: Int32
class func create(in context: NSManagedObjectContext, from json: [String: Any]) -> Foo {
let foo = NSEntityDescription.insertNewObject(forEntityName: "Foo", into: context) as! Foo
foo.title = json["title"] as! String
foo.subtitle = json["subtitle"] as? String
foo.id = json["id"] as! Int32
return foo
}
}
有没有一种简单的方法可以将这样的内容添加到核心数据定义的实体中 是的,在任何情况下,您都必须调用指定的初始值设定项
NSManagedObject/NSEntityDescription
大概是这样的:
convenience init?(json: [String: Any]) {
let id = json["id"] as! Int
let title = json["title"] as! String
let subtitle = json["subtitle"] as? String
self.init(
anId: id,
aTitle: title,
aSubtitle: subtitle,
)
}
class Foo: NSManagedObject {
@NSManaged var title: String
@NSManaged var subtitle: String?
@NSManaged var id: Int32
class func create(in context: NSManagedObjectContext, from json: [String: Any]) -> Foo {
let foo = NSEntityDescription.insertNewObject(forEntityName: "Foo", into: context) as! Foo
foo.title = json["title"] as! String
foo.subtitle = json["subtitle"] as? String
foo.id = json["id"] as! Int32
return foo
}
}
create(在
中)是一个类方法,您可以调用它
let context = appDelegate.managedObjectContext
let newItem = Foo.create(in: context, from: jsonDictionary)
appDelegate
是appDelegate
实例,jsonDictionary
是[String:Any]
字典。是的,在任何情况下,您都必须调用NSManagedObject/NSEntityDescription
的指定初始值设定项
大概是这样的:
convenience init?(json: [String: Any]) {
let id = json["id"] as! Int
let title = json["title"] as! String
let subtitle = json["subtitle"] as? String
self.init(
anId: id,
aTitle: title,
aSubtitle: subtitle,
)
}
class Foo: NSManagedObject {
@NSManaged var title: String
@NSManaged var subtitle: String?
@NSManaged var id: Int32
class func create(in context: NSManagedObjectContext, from json: [String: Any]) -> Foo {
let foo = NSEntityDescription.insertNewObject(forEntityName: "Foo", into: context) as! Foo
foo.title = json["title"] as! String
foo.subtitle = json["subtitle"] as? String
foo.id = json["id"] as! Int32
return foo
}
}
create(在
中)是一个类方法,您可以调用它
let context = appDelegate.managedObjectContext
let newItem = Foo.create(in: context, from: jsonDictionary)
appDelegate
是appDelegate
实例,jsonDictionary
是[String:Any]
字典。只需使用一个方便的初始化器扩展您的类:
convenience init(
context moc: NSManagedObjectContext,
json: [String: Any]
) {
self.init(context: moc)
id = json["id"] as? Int
title = json["title"] as? String
subtitle = json["subtitle"] as? String
}
只需使用方便的初始化器扩展您的类:
convenience init(
context moc: NSManagedObjectContext,
json: [String: Any]
) {
self.init(context: moc)
id = json["id"] as? Int
title = json["title"] as? String
subtitle = json["subtitle"] as? String
}
我必须做一些其他的事情。在xcdatamodel中的DataModel检查器中,我必须将codegen设置为Category/Extension,并且必须添加
@objc(Foo)
在类定义上方。显然还需要导入CoreData。我还需要做一些其他事情。在xcdatamodel中的DataModel检查器中,我必须将codegen设置为Category/Extension,并且必须在类定义上方添加@objc(Foo)
。显然还需要导入CoreData。