Not sure why I get "Missing sentinel in function call?"
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: @"a", @"b", @"cat", @"dog", nil];
ppp = [NSMutableArray arrayWithCapacity:3];
[ppp addObject:[[NSMutableArray alloc] initWithObjects: kkk]]; // <<--- Missing sentinel in functio开发者_如何学编程n call
[ppp addObject:[[NSMutableArray alloc] initWithObjects: kkk, nil]]; //<<--- change, but it falls out
NSLog(@"Working: %@ %@", [[ppp objectAtIndex:0] objectAtIndex:3], [[ppp objectAtIndex:0] objectAtIndex:2] );
initWithObjects:
must be terminated with a trailing nil
. Since it is a single object, you should be a able to use initWithObject:
. That said, you will be leaking the array like this. Do
[ppp addObject:[NSMutableArray arrayWithObject:kkk]];
There is one more problem with the piece of code here,
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: @"a", @"b", @"cat", @"dog", nil];
ppp = [NSMutableArray arrayWithCapacity:3];
[ppp addObject:[[NSMutableArray alloc] initWithObjects: kkk, nil]];
You are creating a three dimensional array. So
NSLog(@"Working: %@ %@", [[ppp objectAtIndex:0] objectAtIndex:3], [[ppp objectAtIndex:0] objectAtIndex:2] );
is wrong.
NSLog(@"Working: %@ %@", [[[ppp objectAtIndex:0] objectAtIndex:0] objectAtIndex:3], [[[ppp objectAtIndex:0] objectAtIndex:0] objectAtIndex:2] );
should log proper values.
However if you need a two dimensional array based on your log statement, I would say you need to do this instead,
[ppp addObject:kkk];
You need to add nil
as the last object in the list.
[ppp addObject:[[NSMutableArray alloc] initWithObjects: kkk, nil]];
Basically it tells the method to stop looking for more objects. Without that, it can look at a bad pointer and crash.
精彩评论