-(id) initWithData:(NSString *)lastName: (NSString *)firstName{
self->firstname = firstName;
self->lastname = lastName;
return self;
}
-(void) printData {
NSLog(@"firstname: %@", firstname);
NSLog(@"lastname: %@", lastname);
}
so whenever I create a new object using the above init function. And Add objects to a NSMutableArray, using the addObject function.
NSMutableArray *objectArray = [[NSMutableArray alloc] init];
CustomObject *tempObject = [[CustomObject alloc] initWithData: @"smith": @"john"];
CustomObject *tempObjectb = [[CustomObject alloc] initWithData: @"brown": @"william"];
[objectArray addObject:tempObject];
[objectArray addObject:tempObjectb];
[[objectArray objectAtIndex:0] printData];
[[objectArray objectAtIndex:1] printData];
objects at index 1, and 0 always equal the whichever object was added to the array last.
This also happens if I use a for loop, or have more than 2 objects, all values when printed, turn to the values of the last added object to the objectArray. Let me know if there is any information that I am missing.
Is there something that I am missing?
initWithData
: if ( self = [super init] { .. rest comes here .. } return self; }. I assume your custom object inherits from NSObject, therefore you have to call the [super init]. – iska Dec 7 '11 at 3:36