错误:索引1超出空数组的边界

时间:2014-11-04 12:09:34

标签: objective-c xcode nsmutablearray

我创建了一个NSMutableArray来存储一些角色。当我在那里添加一些角色时,它给了我一个错误。这是我的代码:

@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;
}    

1 个答案:

答案 0 :(得分:0)

请改用[actions addObject:@"X"]。如果数组没有元素,则无法在索引1处插入对象。