当我使用NSString对象初始化而不是手动输入文本时,为什么我不能访问NSMutableArray的第4个元素?
例如:
NSString * d = @"d";
self.arr = [[NSMutableArray alloc] initWithObjects:
[NSMutableArray arrayWithObjects:@"A",@"B",@"C",d,nil],
[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"d",nil],
nil];
这会导致超出边界错误(NSString):
NSMutableArray *subArray = [self.arr objectAtIndex:0];
question = [subArray objectAtIndex:3];
这不是(@“d”):
NSMutableArray *subArray = [self.arr objectAtIndex:1];
question = [subArray objectAtIndex:3];
为什么会这样?当然他们是一回事吗?我希望减少写“@ d”的次数。
答案 0 :(得分:1)
他们应该是一样的...我在我的项目中尝试过。它运作良好。
NSString * d = @"d";
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
[NSMutableArray arrayWithObjects:@"A",@"B",@"C",d,nil],
[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"d",nil],
nil];
NSLog(@"0 array 4th:%@",[[array objectAtIndex:0] objectAtIndex:3]);
NSLog(@"1 array 4th:%@",[[array objectAtIndex:1] objectAtIndex:3]);
输出如下:
2013-12-16 17:05:30.092 Demo[1005:60b] 0 array 4th:d
2013-12-16 17:05:30.094 Demo[1005:60b] 1 array 4th:d