我想在SQLite中创建一个新的实体(表).我的代码如下:
+(BOOL)CreateDataSet:(NSManagedObjectModel *) model
attributes:(NSDictionary*)attributes
entityName:(NSString*) entityName
{
NSEntityDescription *entityDef = [[NSEntityDescription alloc] init];
[entityDef setName:entityName];
[entityDef setManagedObjectClassName:entityName];
[model setEntities:[NSArray arrayWithObject:entityDef]];
NSArray *properties = [CoreDataHelper CreateAttributes:attributes];
[entityDef setProperties:properties];
[entityDef release];
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
但它会引发错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Can't modify an immutable model.'
*** Call stack at first throw:
(
0 CoreFoundation 0x01c5abe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01daf5c2 objc_exception_throw + 47
2 CoreData 0x0152634a -[NSManagedObjectModel(_NSInternalMethods) _throwIfNotEditable] + 106
3 CoreData 0x01526904 -[NSManagedObjectModel setEntities:] + 36
....
这似乎表明该模型是只读的.
我引用苹果的文档,它描述得非常准确:
实体描述在被对象图管理器使用之前是可编辑的。这允许您动态创建或修改它们。然而,一旦使用了描述(当它所属的托管对象模型与持久存储协调器相关联时),就不能(实际上不能)更改它。这是在运行时强制执行的:在模型与持久存储协调器关联后,任何尝试改变模型或其任何子对象都会导致抛出异常。如果需要修改正在使用的模型,请创建一个副本,修改该副本,然后丢弃旧模型中的对象。
| 归档时间: |
|
| 查看次数: |
4779 次 |
| 最近记录: |