Reputation: 225
My array contain (firstName,lastName, age) at each index. now I want to change age of a person at index 1. please guide how to do it. Here is my code.
- (void)viewDidLoad {
[super viewDidLoad];
productArray=[[NSMutableArray alloc]init];
PersonDetail *personObj = [[PersonDetail alloc] init];
personObj.firstName = @"Adeem";
personObj.lastName = @"Basraa";
personObj.age = @"5";
[productArray addObject:personObj];
[personObj release];
PersonDetail *personObj = [[PersonDetail alloc] init];
personObj.firstName = @"Ijaz";
personObj.lastName = @"Ahmed";
personObj.age = @"10";
[productArray addObject:personObj];
[personObj release];
PersonDetail *personObj = [[PersonDetail alloc] init];
personObj.firstName = @"Waqas";
personObj.lastName = @"Ahmad";
personObj.age = @"15";
[productArray addObject:personObj];
[personObj release];
}
Upvotes: 0
Views: 7875
Reputation: 3464
You should use NSMutableDictionary
to save name,age,phone,address and add this dictionary to your array.
[[arry objectAtIndex:1] setObject:@"value" forKey:@"key"];
Like this you can change value.
Upvotes: 4
Reputation: 1282
Perhaps this will help you.
- (void)viewDidLoad {
[super viewDidLoad];
productArray=[[NSMutableArray alloc]init];
PersonDetail *personObj = [[PersonDetail alloc] init];
personObj.firstName = @"Adeem";
personObj.lastName = @"Basraa";
personObj.phoneNumber = @"123456789";
[productArray addObject:personObj];
[personObj release];
PersonDetail *personObj = [[PersonDetail alloc] init];
personObj.firstName = @"Ijaz";
personObj.lastName = @"Ahmed";
personObj.phoneNumber = @"987654321";
[productArray addObject:personObj];
[personObj release];
PersonDetail *personObj = [[PersonDetail alloc] init];
personObj.firstName = @"Waqas";
personObj.lastName = @"Ahmad";
personObj.phoneNumber = @"45656789";
[productArray addObject:personObj];
[personObj release];
}
- (void)change {
for (int i=0;i<[productArray count];i++) {
PersonDetail *personObj = (PersonDetail *)[[productArray objectAtIndex:i] retain];
personObj.phoneNumber = @"New";
}
}
Upvotes: 2
Reputation: 1658
try this
-insertObject:atIndex: or replaceObjectAtIndex:withObject:
Upvotes: 2
Reputation: 13713
Person *person = (Person *)[myArray objectAtIndex:1];
person.age = 2;
Assuming Person
is your custom object stored in the array
Upvotes: 5
Reputation: 11037
use this
[arrname replaceObjectAtIndex:1 withObject:[NSNumber numberWithInt:13]];//13 is the age you want to change
Upvotes: 4
Reputation: 22820
Supposing your NSMutableArray
consists of NSMutableDictionary
objects,
try something like this :
NSMutableDictionary* entry = [entries objectAtIndex:index];
[entry setValue:@"newAge" forKey:@"Age"];
Upvotes: 1