我想在SQLite中創建一個新的實體(表)。我的代碼如下:如何通過CoreData模型創建一個新的實體(表)?
+(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;
}
但它引發的錯誤:
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 ....
這似乎表明,該模型是隻讀的。
警告:核心數據不是SQL。實體不是表格。對象不是行。列不是屬性。核心數據是一個對象圖管理系統,它可能會或可能不會持久化對象圖,並且可能會或可能不會在後臺使用SQL來做到這一點。試圖用SQL語言來思考覈心數據會導致你完全誤解核心數據,並導致很多悲傷和浪費時間。 – TechZen 2011-03-17 19:54:23