NSArray *planetArray = [NSArray arrayWithObjects:@"Earth",
@"Jupiter",
@"Saturn",
@"Neptune",
@"Pluto", nil];
NSMutableArray *objectArray = [[NSMutableArray alloc] init];
for(NSString *eachPlanet in planetArray) {
Planet *newPlanet = [[Planet alloc] init];
[newPlanet setValue:eachPlanet forKey:@"name"];
[newPlanet setValue:@"TEST" forKey:@"type"];
[newPlanet setValue:[NSNumber numberWithInt:1234] forKey:@"mass"];
[objectArray addObject:newPlanet];
[newPlanet release];
}
for(Planet *displayEachPlanet in objectArray) {
NSLog(@"DATA: %@", displayEachPlanet);
}
[objectArray release];
I am curious if this is the best way to create an object and set an iVar for each item in an array. Basically I am:
- Creating a Planet object
- Setting the iVar (from the NSString array)
- Adding the Planet object to an array.
Releasing the Planet object
Printing my Planet objects
Releasing the array
NB: I am just testing, this is not for anything, I was just curious ...
cheers Gary