使用NSMutableArray更改开关案例。目标C.

时间:2016-08-29 19:05:41

标签: ios objective-c xcode

我有一个NSMutableArray,其中包含一系列数字,我想在switch中更改个案。并声明了所有变量。无论我尝试什么,应用程序总是崩溃。 如何将int从数组传递到switch

 -(NSMutableArray*)tasks {
     tasks = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInteger:1] ,[NSNumber numberWithInteger:2] ,[NSNumber numberWithInteger:3] ,[NSNumber numberWithInteger:4] ,[NSNumber numberWithInteger:5] ,.........,nil];
     return tasks;
     }

- (IBAction)random:(id)sender {
    text = [[tasks objectAtIndex:x] integerValue];
    x++;

        switch (text) {


            case 0:
               //something
                break;

            case 1:
                //something
                break;

            case 2:
                //something
                break;
.
.
.
.
.
            case 22:
               //something
                break;   
            default:

        }  }

1 个答案:

答案 0 :(得分:0)

你能试试吗?它应该工作。

-(NSMutableArray*) tasks {
    NSMutableArray *tasks = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInteger:1] ,[NSNumber numberWithInteger:2] ,[NSNumber numberWithInteger:3] ,[NSNumber numberWithInteger:4] ,[NSNumber numberWithInteger:5],nil];
    return tasks;
}

- (IBAction)random:(id)sender {
    int x = 0;
    NSInteger text = [[self.tasks objectAtIndex:x] integerValue];
    x++;

    switch (text) {


        case 0:
            //something
            break;

        case 1:
            //something
            break;

        case 2:
            //something
            break;
        case 22:
            //something
            break;
        default:
            ;

    }
}