试图在iOS中基于NSString值动态创建NSManagedObject子类 [英] Trying to dynamically create an NSManagedObject subclass based on NSString value in iOS

查看:175
本文介绍了试图在iOS中基于NSString值动态创建NSManagedObject子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个利用Core Data的应用程序,它在数据模型中有多个实体。我想做的是创建一个方法,它能够创建NSManagedObject的合适的子类,基于它接收的方法的名称,这将是一个NSString。

I am building an application that utilizes Core Data, which has multiple entities in the Data Model. What I would like to do is create a method which is able to create the appropriate subclass of NSManagedObject based on the name that the method it receives, which would be an NSString.

我的方法如下:

- (NSManagedObject *) addEntity:(NSString *)name {

  NSManagedObjectContext *context = [self managedObjectContext];
  NSError *error;

  //need to add a line here that casts EntityType to of type "name" which is passed into the method.
  EntityType *testEntity = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:context];

  [context save:&error];

   return testEntity;

}

其中EntityType类型为name如果我将名称Manager传递给方法,我将创建一个类型为Manager的对象。因此上面这行:

Where "EntityType" is of type "name", such that if I pass the name "Manager" into the method, I will create an object of type "Manager". Therefore the above line:

EntityType *testEntity = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:context];

会读为:

Manager *testEntity = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:context];

我需要做什么来动态创建实体基于我传入的类型方法?请注意,我这样做是因为我的应用程序中有超过二十个实体,并希望只有一个方法,我可以使用它们中的任何一个。

What do I need to do in order to dynamically create Entities based on the type that I pass into the method? Please note that I am doing this because I have over twenty entities in my application and would like to have just one method that I can use for any of them.

提前感谢所有回复。

推荐答案

问题是 testEntity 的类型只是使用通用 NSManagedObject

If your issue is the type of testEntity just use a generic NSManagedObject

无论如何一个很好的解决方案是添加一个类别 NSManagedObject 。这里有一个实现,它基本上复制了由MagicalRecord提供的相同的功能。

Anyway a nice solution would be to add a category over NSManagedObject. Here's an implementation which basically replicates the same functionality provided by MagicalRecord.

@interface NSManagedObject (Utilities)
+ (instancetype)my_createAndSave:(NSError **)error;
@end






NSManagedObject + Utilities。 m




NSManagedObject+Utilities.m

@implementation NSManagedObject (Utilities)
+ (instancetype)my_createAndSave:(NSError **)error {
    NSString *entityName = NSStringFromClass(self);
    if ([self respondsToSelector:@selector(entityName)]) {
        entityName = [self performSelector:@selector(entityName)];
    }
    NSManagedObject *entity = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.managedObjectContext];
    [self.managedObjectContext save:error];
    return entity;
}
@end

几句话:


  • 命名

  • being a category is always preferable to add a custom prefix, not to clash with Apple's API. I used my_ here, but you can add whatever you like.
  • starting the method name with new should be reserved to method returning a non autoreleased object. Since entity is autoreleased, ARC will add an extra retain before returning the object (balanced later by a release) (reference). While this is not an issue, it breaks the standard naming conventions and should be avoided.

错误处理


  • 您应该产生错误或正确处理它。我选择在我的实现中将错误传回调用者

这篇关于试图在iOS中基于NSString值动态创建NSManagedObject子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆