程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-報錯:NSNumber declares the selector initwithInt

ios-報錯:NSNumber declares the selector initwithInt

編輯:編程綜合問答
報錯:'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;
        }
    }
}

請高手指教,謝謝。

最佳回答:


沒有這個initwithInt:,有一個initWithInt:, Objective-C 區分大小寫

代碼中的 [ 放錯了

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

應該是:

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

最好改為:

[questionArray addObject:[NSNumber numberWithInt:i]]; 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved