Kri*_*son 2 cocoa-touch objective-c
不知道为什么我会在函数调用中找到"缺少哨兵?"
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: @"a", @"b", @"cat", @"dog", nil];
ppp = [NSMutableArray arrayWithCapacity:3];
[ppp addObject:[[NSMutableArray alloc] initWithObjects: kkk]]; // <<--- Missing sentinel in function 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] );
Run Code Online (Sandbox Code Playgroud)
initWithObjects:必须以尾随终止nil.由于它是单个对象,因此您应该能够使用initWithObject:.也就是说,你会像这样泄漏数组.做
[ppp addObject:[NSMutableArray arrayWithObject:kkk]];
Run Code Online (Sandbox Code Playgroud)
这段代码还有一个问题,
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: @"a", @"b", @"cat", @"dog", nil];
ppp = [NSMutableArray arrayWithCapacity:3];
[ppp addObject:[[NSMutableArray alloc] initWithObjects: kkk, nil]];
Run Code Online (Sandbox Code Playgroud)
您正在创建一个三维数组.所以
NSLog(@"Working: %@ %@", [[ppp objectAtIndex:0] objectAtIndex:3], [[ppp objectAtIndex:0] objectAtIndex:2] );
Run Code Online (Sandbox Code Playgroud)
是错的.
NSLog(@"Working: %@ %@", [[[ppp objectAtIndex:0] objectAtIndex:0] objectAtIndex:3], [[[ppp objectAtIndex:0] objectAtIndex:0] objectAtIndex:2] );
Run Code Online (Sandbox Code Playgroud)
应记录正确的值.
但是,如果您需要基于日志语句的二维数组,我会说您需要这样做,
[ppp addObject:kkk];
Run Code Online (Sandbox Code Playgroud)