子类NSManagedObject易于初始化

时间:2017-01-04 14:48:33

标签: ios core-data nsmanagedobject

对于来自服务器API的模型,我使用类似的构造函数:

@implementation MenuAPIModel

-(instancetype)initWithServerData:(id)data{

    self = [super initWithServerData:data];
    return self;
}

-(void)performMapping{

    _basketId = self.serverData[@"basket_id"];
    _orderId = self.serverData[@"order_id"];
    _itemId = self.serverData[@"id"];
    _name = self.serverData[@"name"] == [NSNull null] ? @"" : self.serverData[@"persons"];
    _payedFrom = [self.dfDetail dateFromString:self.serverData[@"payed_from"]] == nil ? [self.dfBasic dateFromString:self.serverData[@"payed_from"]] : [self.dfDetail dateFromString:self.serverData[@"payed_from"]];
    _payedTill = [self.dfBasic dateFromString:self.serverData[@"payed_till"]] == nil ? [self.dfBasic dateFromString:self.serverData[@"payed_till"]] : [self.dfDetail dateFromString:self.serverData[@"payed_till"]];
    _persons = self.serverData[@"persons"] == [NSNull null] ? @"2-3" : self.serverData[@"persons"];
    _price = self.serverData[@"price"] == [NSNull null] ? @(0) : self.serverData[@"price"];
    _price3 = self.serverData[@"price3"] == [NSNull null] ? @(0) : self.serverData[@"price3"];
    _price12 = self.serverData[@"price12"] == [NSNull null] ? @(0) : self.serverData[@"price12"];
    _status = self.serverData[@"status"] == [NSNull null] ? @"not_payed" : self.serverData[@"persons"];
}

所以,我可以创建简单的对象:[MenuAPIModel initWithServerData:data];

现在我继承了NSManagedObject并且想知道,我怎样才能将这个4个文件子类化为XCode为我创建的,构建如上所述的构造函数?

2 个答案:

答案 0 :(得分:1)

您可以创建一种可以具有以下实现的帮助器(这只是示例,因此我使用了Swift):

static open func createObject <T : NSManagedObject> (_ objectType : T.Type, record : Dictionary<String, Any>? = nil, context : NSManagedObjectContext) -> (T)
{
    let newObject = NSEntityDescription.insertNewObject(forEntityName: String(describing: objectType), into: context)

    // Error never occured

    var typedNewObject = newObject as! T

    guard let trueRecord = record
    else
    {
        return typedNewObject
    }

    typedNewObject = updateObject(typedNewObject, record: trueRecord, context: context)

    return typedNewObject
}

static open func updateObject <T : NSManagedObject> (_ object : T, record : Dictionary<String, Any>, context : NSManagedObjectContext) -> (T)
{
    for (key, value) in record
    {
        setValue(value, key: key, object: object)
    }

    return object
}

static public func setValue (_ value : Any, key : String, object : NSManagedObject) -> (Void)
{
    object.setValue(value, forKey: key)
}

使用此类方法,您可以输入:

let myNewObject = Helper.createObject(YourClassName.self, record: yourDictionary, context: yourContext) // Helper, or any other class name you give it.

我希望它足够接近你需要的东西。

答案 1 :(得分:1)

我不完全理解您想要子类化的内容,但是为了使用CoreData,您的模型必须子类NSManagedObject,所以如果您想要子类化您编写的自定义模型类,该类必须是NSManagedObject的子类。

现在,NSManagedObject的新XCode模板为您在xcdatamodel中声明的每个模型创建一个类和一个类别。你会有这样的事情。

<强> MenuAPIModel + CoreDataProperties.h

@interface MenuAPIModel (CoreDataProperties) {
}
@property(nonatomic, strong) NSNumber *basketId
.....other properties that you have
@end

<强> MenuAPIModel + CoreDataProperties.m

@implementation MenuAPIModel (CoreDataProperties)
@dynamic basketId
@dynamic ....other properties ...
@end

<强> MenuAPIModel.h

@interface MenuAPIModel : NSManagedObject
+(instancetype)initWithServerData:(id)serverData;
@end

<强> MenuAPIModel.m

@implementation MenuAPIModel

 +(instancetype)initWithServerData:(id)serverData {
     NSManagedObjectContext *context = ...get the current context...
     MenuAPIModel *model = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([MenuAPIModel class])inManagedObjectContext:context];
     model.basketId = serverData[@"basket_id"];
     ...other parsing....
     return model;
 }

正如您所看到的,您可以使用这些类&amp;默认情况下由XCode创建的类别,只需添加一个额外的方法。只是提醒一下,NSManagedObject无法使用alloc] init];创建,必须使用NSManagedObjectContext创建。