CoreData - should I pass an existing NSNumber to set the attribute of an entity, or create a new one?
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 obj开发者_如何学编程ectAtIndex: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
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]
}
精彩评论