JsW
JsW

Reputation: 1758

How to change a value in a two dimensions NSMutableArray?

I'm new to OC.
In Swift, it's really easy to change a value in a two dimensions array. Just like this a[0][0] = "1"

But I'm really confused now about how to do it in OC.
Thank you for any advice.

self.infos = 
[NSMutableArray arrayWithObjects: [NSMutableArray arrayWithObjects:@"11", @"22", @"33", @"44", @"55", @"66", nil], 
[NSMutableArray arrayWithObjects:@"aa", nil], nil];

For example. how to set aa to bb?

BTW, is it right way to declare infos in @interface?

@property (strong, nonatomic) NSMutableArray* infos;

Upvotes: 0

Views: 63

Answers (3)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Try simple

   self.infos[1][0] = @"newValue";

or Complex

  NSArray*fr = [self.infos objectAtIndex:1];

 [fr replaceObjectAtIndex:0 withObject:@"newValue"];

Upvotes: 3

J.Hunter
J.Hunter

Reputation: 586

Like this:

    NSMutableArray *infos = [NSMutableArray arrayWithObjects:
                         [NSMutableArray arrayWithObjects:@"11", @"22", @"33", @"44", @"55", @"66", nil],
                         [NSMutableArray arrayWithObjects:@"aa", nil],
                         nil];

[infos[0] setObject:@"1" atIndexedSubscript:0];

or

infos[0][1] = @"2";

Upvotes: 0

Mohamad Bachir Sidani
Mohamad Bachir Sidani

Reputation: 2099

BTW, you can actually write this:

infos[1][0] = @"bb";

Upvotes: 0

Related Questions