0

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;

2
  • 1
    infos[1] is a NSArray, not a NSMutableArray Is they are indeed mutable, then you can do self.infos[1][0] = @"bb". It's missing a * in @property (strong, nonatomic) NSMutableArray *infos; (XCode should throw an error) Commented Dec 14, 2017 at 12:17
  • @Larme sorry for too much typo. Commented Dec 14, 2017 at 12:20

3 Answers 3

3

Try simple

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

or Complex

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

 [fr replaceObjectAtIndex:0 withObject:@"newValue"];
Sign up to request clarification or add additional context in comments.

Comments

0

BTW, you can actually write this:

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

3 Comments

Oh god! It's Great! But why there are so many complicated methods for accessing?(like remove, replace...)
Glad it helped! The reason might be that Objective C deals with pointers not only objects (unlike swift). If you think this is the right answer please mark it as correct Thanks!
Thanks. But it seems sh_Khan answered a little bit earlier. Thanks again.
0

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

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.