I just got started with core data. I have bunch of entities with about 40-50 attributes. Given that I have an array of strings which represent the name of the attributes (the type of these attributes are all string), is there a way I can programmatically set the attributes from the array? or do I have to manually copy paste these attributes into the Xcode IDE?
You can create your model programatically. The Xcode IDE is just what's easiest for most people.
Create your model with
NSManagedObjectModel *mom = [[[NSManagedObjectModel alloc] init] autorelease];
Then create your entities with
NSMutableArray *entities = [NSMutableArray array];
NSEntityDescription *event = [[[NSEntityDescription alloc] init] autorelease];
[entities addObject:event];
[event setName:@"Event"];
[event setProperties:eventProperties];
...
Then add the entities to the model:
[mom setEntities:entities];
Note that you can't change the model once you're using it with a store / context.
Once you've created your model, you can save it using NSCoding.
This is also shown in the Core Data Utility Tutorial.
While fiddling with the sqlite data store will not help you, there is a file that describes the data model inside your project folder. This file is in xml format so you could iterate through your array and generate the necessary entries into this file. You could then overwrite the default file and reopen your project in Xcode. If you are lucky all your entities and attributes will be there.
To get you started let me point you to the file. If you just start a new project with the default core data model included, it should be located at
<project directory>/<project name>/project_name.xcdatamodeld/project_name.xcdatamodel
The default looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model name="Test1.xcdatamodel" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="Event">
<attribute name="timeStamp" optional="YES" attributeType="Date"/>
</entity>
<elements>
<element name="Event" positionX="261" positionY="189" width="128" height="60"/>
</elements>
</model>
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With