0
我創建了一個NSMutableArray
來在那裏存儲一些字符。當我添加一些字符時,它會給我一個錯誤。這裏我的代碼:錯誤:索引1超出了空數組的邊界
@property(strong,nonatomic) NSMutableArray *actions; < ViewController.h
@synthesize actions; < ViewController.m
actions = [[NSMutableArray alloc]init];
switch ([sender tag]) {
case 0:
currentLabel = water;
[actions insertObject:@"0" atIndex:i];
i++;
break;
case 1:
currentLabel = coke;
[actions insertObject:@"1" atIndex:i];
i++;
break;
case 2:
currentLabel = fanta;
[actions insertObject:@"2" atIndex:i];
i++;
break;
}
顯然,如果數組中沒有「i」元素,則不能在索引「i」處插入元素。 – holex 2014-11-04 12:13:51
第一次調用方法'insertObject:','i'是1,它應該是零。 – KudoCC 2014-11-04 12:14:55
'i'初始化爲'0' – 12oni 2014-11-04 17:20:16