2011-11-16 78 views

回答

1

您要修改的部分是NSManagedObjectModel。但是,一旦它被使用就不能被修改,因为它改变了数据库模式。您必须复制模型,修改模型,转换所有数据,然后切换到新模型。以下页面应该是有帮助的:

+0

谢谢,但我需要更多关于Programetically创建核心数据模型和属性的明确信息。 – iosfanboy9

0

抱歉,但是这是不可能的。

您需要根据您的要求将实体和属性设置为static,并且只使用您在运行时需要的属性。

0

也许如果你创建你的核心数据模型的键/值的实体。

例如: CarEntity有2个属性:键/值(两个类型的字符串)

值在CarEntity可以是:

  • “模型”, “VW”
  • “电源” “7千瓦”
  • “任何键”, “任何价值”

会为你工作?

0

您可以在将ManagedObjectModel分配给NSPersistentStoreCoordinator之前创建/修改实体,这里是一个示例代码。我们可以通过下面的例子来说明如何使用NSURL * modelURL = [[NSBundle mainBundle] URLForResource:@“CoreDataDemoModel”withExtension:@“momd”]; NSManagedObjectModel * mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSEntityDescription *runEntity = [[NSEntityDescription alloc] init]; 
[runEntity setName:@"Run"]; 
[runEntity setManagedObjectClassName:@"Run"]; 
[mom setEntities:@[runEntity]]; 

NSMutableArray *runProperties = [NSMutableArray array]; 

NSAttributeDescription *dateAttribute = [[NSAttributeDescription alloc] init]; 
[runProperties addObject:dateAttribute]; 
[dateAttribute setName:@"date"]; 
[dateAttribute setAttributeType:NSDateAttributeType]; 
[dateAttribute setOptional:NO]; 

NSAttributeDescription *idAttribute= [[NSAttributeDescription alloc] init]; 
[runProperties addObject:idAttribute]; 
[idAttribute setName:@"processID"]; 
[idAttribute setAttributeType:NSInteger32AttributeType]; 
[idAttribute setOptional:NO]; 
[idAttribute setDefaultValue:@0]; 

[runEntity setProperties:runProperties]; 

NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom]; 

NSError *error = nil; 
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataDemoModel.sqlite"]; 
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]; 
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; 
[context setPersistentStoreCoordinator:store];