0

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];

}

2
  • By using key value you can replace the content at any index of nsmutable array. Commented Apr 18, 2012 at 6:47
  • you're missing some info regarding the structure… Commented Apr 18, 2012 at 6:47

6 Answers 6

5
Person *person = (Person *)[myArray objectAtIndex:1];
person.age = 2;

Assuming Person is your custom object stored in the array

Sign up to request clarification or add additional context in comments.

3 Comments

creepy - same type. same name. same age :)
well you win .. you posted before me :)
actually, you beat me by 1 minute. i'll delete mine =)
4

use this

[arrname replaceObjectAtIndex:1 withObject:[NSNumber numberWithInt:13]];//13 is the age you want to change

Comments

4

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.

Comments

2

try this

   -insertObject:atIndex: or replaceObjectAtIndex:withObject:

Comments

2

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";
    }
}

Comments

1

Supposing your NSMutableArray consists of NSMutableDictionary objects,

try something like this :

NSMutableDictionary* entry = [entries objectAtIndex:index];

[entry setValue:@"newAge" forKey:@"Age"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.