不知道为什么会收到“函数调用中缺少前哨?”

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] );

最佳答案

initWithObjects:必须以结尾的nil终止。由于它是单个对象,因此您应该能够使用initWithObject:。也就是说,您将像这样泄漏数组。做

[ppp addObject:[NSMutableArray arrayWithObject:kkk]];


这里的代码还有一个问题,

NSMutableArray *kkk = [NSMutableArray arrayWithObjects: @"a", @"b", @"cat", @"dog", nil];
ppp = [NSMutableArray arrayWithCapacity:3];
[ppp addObject:[[NSMutableArray alloc] initWithObjects: kkk, nil]];


您正在创建三维数组。所以

NSLog(@"Working: %@   %@", [[ppp objectAtIndex:0] objectAtIndex:3], [[ppp objectAtIndex:0] objectAtIndex:2] );


是错的。

NSLog(@"Working: %@   %@", [[[ppp objectAtIndex:0] objectAtIndex:0] objectAtIndex:3], [[[ppp objectAtIndex:0] objectAtIndex:0] objectAtIndex:2] );


应该记录正确的值。

但是,如果您需要基于log语句的二维数组,我会说您需要这样做,

[ppp addObject:kkk];

关于objective-c - 在函数调用中缺少哨兵?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6702564/

10-13 03:55