Как динамически создать новую сущность (таблицу) с помощью модели CoreData?

Я хочу создать новую сущность (таблицу) в 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;
}

Но он выдает ошибки:

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
....

Кажется, это показывает, что модель доступна только для чтения.

6
задан Shaggy Frog 17 March 2011 в 16:25
поделиться