2 annbnncnnd AnnBnnCnnD 于 2013.08.07 14:16 提问

报错:'NSNumber' declares the selector initwithInt'

做了一个不同类别的测试应用。代码运行后报错: No visible @interfacefor 'NSNumber' declares the selector initwithInt.'

代码:

-(IBAction)Category1:(id)sender{

    Category1.hidden = YES;
    Category2.hidden = YES;
    Question.hidden = NO;
    Answer1.hidden = NO;
    Answer2.hidden = NO;
    Answer3.hidden = NO;
    Answer4.hidden = NO;
    Right1.hidden = YES;
    Right2.hidden = YES;
    Right3.hidden = YES;
    Right4.hidden = YES;
    Wrong1.hidden = YES;
    Wrong2.hidden = YES;
    Wrong3.hidden = YES;
    Wrong4.hidden = YES;
    SelectCategory.hidden = YES;
    NSMutableArray *questionArray = [[NSMUTABLEArray alloc] init];
    for (int i = 1; i < 101; i++) {
        [questionArray addObject:[NSNumber alloc] initwithInt:i]];  // Here is where the error occurs
    }

    for (int i = 0; i < 100; i++) {
        int randomIndex = arc4random() % [questionArray count];
        int Category1Question = [[questionArray objectAtIndex:randomIndex] intValue];
        [questionArray removeObjectAtIndex:randomIndex];
        switch (Category1Question) {
            case 0:
            Question.text = [NSString stringWithFormat:@"Question here"];
            Right1.hidden = NO;
            Wrong2.hidden = NO;
            Wrong3.hidden = NO;
            Wrong4.hidden = NO;
            Answer1.text = [NSString stringWithFormat:@"Correct answer here"];
            Answer2.text = [NSString stringWithFormat:@"Wrong answer here"];
            Answer3.text = [NSString stringWithFormat:@"Wrong answer here"];
            Answer4.text = [NSString stringWithFormat:@"Wrong answer here"];
            break;
            case 1:
            // etc etc all the way to case 99
           default:
               break;
        }
    }
}

请高手指教,谢谢。

1个回答

g989_1314125
g989_1314125   2013.08.07 16:50
已采纳

没有这个initwithInt:,有一个initWithInt:, Objective-C 区分大小写

代码中的 [ 放错了

[questionArray addObject:[NSNumber alloc] initwithInt:i]]; 

应该是:

[questionArray addObject:[[NSNumber alloc] initWithInt:i]]; 

最好改为:

[questionArray addObject:[NSNumber numberWithInt:i]]; 
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!