I am having problems filling my an NSMutableArray one object at a time. Here is some sample code to illustrate my problem:
Now this produces an output of:
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
Which is quite different to the output I expected:
a
aa
aaa
aaaa
aaaaa
I am a beginner to Objective C and am unsure why this is.
Code:
NSMutableArray* myArray = [[NSMutableArray alloc] init];
NSMutableString* myString = [[NSMutableString alloc] initWithString:@"a"];
for (int i=0;i<5;++i){
[myArray addObject:myString];
[myString appendString:@"a"];
}
for (int i=0;i<5;++i){
NSLog(@"%@",[myArray objectAtIndex:i]);
}
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
Which is quite different to the output I expected:
a
aa
aaa
aaaa
aaaaa
I am a beginner to Objective C and am unsure why this is.
Last edited by a moderator: