views:

1727

answers:

2

Hey,

I have an method that reads an xml file and stores the xml nodes at a certain XPath-path in an NSArray called *nodes. What I want to do is take each one of the items in the array and add it to a core data entity called Category with the attribute of "name".

I have tried a number of different ways of creating the entity but I'm not sure about the correct way to do this effectively. This is the code used to create the NSArray, any ideas on how to implement this? (ignore the NSError, I will fix this in the final version)

- (IBAction)readCategories:(id)sender

{

NSString *xmlString = [resultView string];

NSData *xmlData = [xmlString dataUsingEncoding: NSASCIIStringEncoding];

NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithData:xmlData options:nil error:nil];

//XPath

NSError *err=nil;

NSArray *nodes = [xmlDoc nodesForXPath:@"//member[name='description']/value/string" error:&err];

}

EDIT - My loop code

NSArray *nodes = [xmlDoc nodesForXPath:@"//member[name='description']/value/string" error:&err];
int arrayCount = [nodes count];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSXMLElement *categoryEl;
NSString *new = [catArrayController newObject];
int i;
for (i = 0; i < arrayCount; i++)
{
 [categoryEl = [nodes objectAtIndex:i]];
 [new setValue:[categoryEl stringValue] forKey:@"name"];
 [catArrayController addObject:new];
}
[pool release];
+1  A: 

What part are you having trouble with? There shouldn't be much more to it than looping through the array, creating a new managed object for each entry, and setting the correct attributes. You can create the managed object with NSEntityDescription's -insertNewObjectForEntityForName:inManagedObjectContext: method.

Marc Charbonneau
In my testing I dropped the core data just so that I could learn to loop through the array. I have updated my question with the new array code but it doesnt work. Whats the best way to loop through the array and add it to an entity?
Alex Mills
+2  A: 

Here's how I'd write it:

for (NSXMLElement *categoryElement in nodes) {
    NSManagedObject *newObject = [catArrayController newObject];
    [newObject setValue:[categoryElement stringValue] forKey:@"name"];
    [catArrayController addObject:newObject];
    [newObject release];
}

First, I'm using the Objective-C 2.0 for-each syntax. This is simpler than using index variables. I eliminated i and arrayCount.

Next, I took out your NSAutoreleasePool. None of the objects in the loop are autoreleased, so it had no effect. (The newObject method returns a retained object which is, by convention, what methods with the word new in their name do) This is also why I release newObject after adding it to the array controller. Since I'm not going to be using it any more in this method, I need to release it.

Also, you had defined new (which I renamed newObject) as an NSString. Core Data objects are always either an instance of NSManagedObject or a subclass of NSManagedObject.

Your line [categoryEl = [nodes objectAtIndex:i]] won't compile. That's because the bracket syntax is used to send a message to an object. This is an assignment statement, so the bracket syntax is not needed here. (This line is also not necessary any more because of I've changed the loop to use the for-each syntax) But, for future reference, categoryEl = [nodes objectAtIndex:i]; would have worked.

Alex