This is my code.
NSString* seats = @"NEWS";
NSMutableString *sp = [[NSMutableString alloc] initWithString:@" "];
NSArray *dials=@[@[sp, sp, sp, sp], @[sp, sp, sp, sp]];
[dials[0][2] replaceCharactersInRange:NSMakeRange(5, 1) withString:[seats substringWithRange:NSMakeRange(3,1)]];
NSLog(@"dial 0 0 : %@",dials[0][0]);
NSLog(@"dial 0 1 : %@",dials[0][1]);
NSLog(@"dial 0 2 : %@",dials[0][2]);
NSLog(@"dial 0 3 : %@",dials[0][3]);
This is my console readout.
2013-02-08 08:23:26.114 [29075:11303] dial 0 0 : S
2013-02-08 08:23:26.115 [29075:11303] dial 0 1 : S
2013-02-08 08:23:26.115 [29075:11303] dial 0 2 : S
2013-02-08 08:23:26.115 [29075:11303] dial 0 3 : S
How can I instead get the following readout, which is what I want?
2013-02-08 08:23:26.114 [29075:11303] dial 0 0 :
2013-02-08 08:23:26.115 [29075:11303] dial 0 1 :
2013-02-08 08:23:26.115 [29075:11303] dial 0 2 : S
2013-02-08 08:23:26.115 [29075:11303] dial 0 3 :
By just doing this:
Got why? If you think well that array just contains duplicate objects. Every pointer is pointing to the same object except dials[0][2], which points to a copied object.
Putting duplicate objects in the same array is allowed, but it may be conceptually wrong. So also consider copying all the objects.
Another solution would be to replace your immutable array with mutable strings with a mutable array with immutable strings, and in this case you could also put duplicate objects, and to change an object you should replace it: