NSStringFromClass([MyEntityClass类])是否生成安全的Core Data Entity名称? [英] Does NSStringFromClass([MyEntityClass class]) generate a safe Core Data Entity name?
问题描述
大多数(所有我看过)Core Data教程使用以下代码片段 @MyEntityClass
硬编码:
Most (all I've seen) Core Data tutorials use the following code snippet with @"MyEntityClass"
hard-coded in:
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MyEntityClass"];
可以安全使用 NSStringFromClass $ c>作为实体名称?
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([MyEntityClass class])];
这个接缝可以更容易处理关于重构等。特别是因为我有Xcode创建我的 NSManagedObject
子类。
This seams to be much easer to deal with regarding refactoring and the like. Especially since I am having Xcode create my NSManagedObject
subclasses. I ask because I have never seen this before, so perhaps I am missing something.
推荐答案
是的,代码是很好的, em>如果您的实体的类在您的模型中设置为 MyEntityClass
。
Yes, that code is fine, if your entity's class is set to MyEntityClass
in your model.
实体类返回实体名称的类方法:
I prefer to give the entity class a class method that returns the entity name:
+ (NSString *)entityName {
return NSStringFromClass(self);
}
并像这样调用:
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:[MyEntityClass entityName]];
这样,如果我想更改类名称而不改变模型中的实体名称,可以只改变类方法:
This way, if I want to change the class name without changing the entity name in the model, I can just make the change in the class method:
+ (NSString *)entityName {
return @"NewEntityName";
}
为什么要这样做?嗯,我可能会为实体决定一个更好的名字。更改类名不会破坏与现有Core Data持久存储的兼容性,但会更改模型文件中的实体名称。我可以改变类名,和 entityName
方法,但保留实体名称在模型中不变,然后我不必担心迁移。 (轻量级迁移支持重命名的实体,所以它不是那么大的交易。)
Why would I do that? Well, I might decide on a better name for the entity. Changing the class name doesn't break compatibility with an existing Core Data persistent store, but changing the entity name in the model file does. I can change the class name, and the entityName
method, but leave the entity name unchanged in the model, and then I don't have to worry about migration. (Lightweight migration supports renamed entities so it's not that big of a deal either way.)
你可以进一步,实际上有 entityName
方法在运行时从托管对象模型中查找实体名称。假设您的应用程序委托具有返回受管对象模型的消息:
You could go further and actually have the entityName
method look up the entity name from the managed object model at runtime. Suppose your application delegate has a message that returns the managed object model:
+ (NSString *)entityName {
static NSString *name;
static dispatch_once_t once;
dispatch_once(&once, ^{
NSString *myName = NSStringFromClass(self);
NSManagedObjectModel *model = [(AppDelegate *)[UIApplication delegate] managedObjectModel];
for (NSEntityDescription *description in model.entities) {
if ([description.managedObjectClassName isEqualToString:myName]) {
name = description.name;
break;
}
}
[NSException raise:NSInvalidArgumentException
format:@"no entity found that uses %@ as its class", myName];
});
return name;
}
显然,如果你真的想这样做, dispatch_once
阻止到一个帮助方法,可能在您的应用程序委托(或你获得模型的任何地方)。
Obviously, if you really want to do this, you should factor out the contents of the dispatch_once
block into a helper method, probably on your app delegate (or wherever you get the model).
这篇关于NSStringFromClass([MyEntityClass类])是否生成安全的Core Data Entity名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!