views:

23

answers:

1

Hello,

I'm starting with CoreData and I have a question : I have an array with NSNumber objects in it. I need to create an entity Event for each object with only one attribute eventNumber which should also be an NSNumber.

Can I pass the object of my array like this :

for (int i = 0, i<[myArray count], i++){
   Event *newEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedContext:managedContext];
   [newEvent setEventNumber:[myArray objectAtIndex:i]]
}
[myArray release]

or is it necessary to create a new NSNumber like that :

for (int i = 0, i<[myArray count], i++){
   Event *newEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedContext:managedContext];
   [newEvent setEventNumber:[NSNumber numberWithInt:[[myArray objectAtIndex:i] intValue]]
}
[myArray release]

Thank you for your help.

Leo

+1  A: 

There's no need to create a new NSNumber for this purpose; your first option is correct.

You could, however, simplify your loop by using fast enumeration:

for (NSNumber *num in myArray) {
   Event *newEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedContext:managedContext];
   [newEvent setEventNumber:num]
}
David Gelhar
allright, I will do that.Thank you for your quick answer !
Leo