我尝试创建一个表情符号UIPikcerView,这是我的代码的一部分:

...
- (void)viewDidLoad {
     [super viewDidLoad];
     _imageArray = @[@"👻", @"👸", @"💩", @"😘", @"🍔", @"🤖", @"🍟", @"🐼", @"🚖", @"🐷"];


     _dataArray1 = [NSMutableArray arrayWithCapacity:100];
     _dataArray2 = [NSMutableArray arrayWithCapacity:100];
     _dataArray3 = [NSMutableArray arrayWithCapacity:100];

     for(int i=0;i < 100; i++){
       [_dataArray1 addObject:[NSNumber numberWithInt:(int)(arc4random() % 10)]];
       [_dataArray2 addObject:[NSNumber numberWithInt:(int)(arc4random() % 10)]];
       [_dataArray3 addObject:[NSNumber numberWithInt:(int)(arc4random() % 10)]];
}
}
...


在UIPickerView委托方法中:

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *pickerLabel = [[UILabel alloc]init];

//debug code
NSNumber *i = [NSNumber numberWithInt:(int)_dataArray1[row]];

if (component == 0) {
    pickerLabel.text = _imageArray[(int)_dataArray1[row]];
}else if (component == 1){
    pickerLabel.text = _imageArray[(int)_dataArray2[row]];
}else{
    pickerLabel.text = _imageArray[(int)_dataArray3[row]];
}

return pickerLabel;
}


当我运行代码时,控制台会弹出“由于未捕获的异常“ NSRangeException”而终止应用程序”。然后添加一条调试代码,就像注释一样

   NSNumber *i = [NSNumber numberWithInt:(int)_dataArray1[row]];


我发现当我等于146时,应用程序崩溃了。
'146'来自。我也在控制台中输入'po _dataArray1',_dataArray1中没有146,有人可以帮我吗?

最佳答案

尝试

[NSNumber numberWithInt:[_dataArray1[row] intValue];


代替

[NSNumber numberWithInt:(int)_dataArray1[row];


将NSNumber强制转换为int有时可能会返回Garbage值。

10-06 11:43