gpt4 book ai didi

ios - pickerViews 崩溃

转载 作者:行者123 更新时间:2023-11-29 01:14:54 25 4
gpt4 key购买 nike

这是我的代码:

@synthesize pickerLetter, pickerNumber, pickerSymbol;


- (void)viewDidLoad {
[super viewDidLoad];
letters = [[NSArray alloc]initWithObjects:@"a", @"b", @"c", nil];
numbers = [[NSArray alloc]initWithObjects:@"1", @"2", @"3", nil];
symbols = [[NSArray alloc]initWithObjects:@"+", @"-", @"/", nil];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return letters.count;
} else if (pickerView == pickerNumber){
return numbers.count;
} else {
return symbols.count;
}
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return [letters objectAtIndex:row];
} else if (pickerView == pickerNumber){
return [numbers objectAtIndex:row];
} else {
return [symbols objectAtIndex:row];
}
}

这是给了我

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

当我在模拟器中使用选择器时。

导致崩溃的代码是

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
label.text = [NSString stringWithFormat:@"%@ %@ %@",[letters objectAtIndex:[pickerLetter selectedRowInComponent:0]],[numbers objectAtIndex:[pickerNumber selectedRowInComponent:1]],[symbols objectAtIndex:[pickerSymbol selectedRowInComponent:2]]];
}

最佳答案

您已将问题缩小到此代码:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
label.text = [NSString stringWithFormat:@"%@ %@ %@",[letters objectAtIndex:[pickerLetter selectedRowInComponent:0]],[numbers objectAtIndex:[pickerNumber selectedRowInComponent:1]],[symbols objectAtIndex:[pickerSymbol selectedRowInComponent:2]]];
}

但是这一行对 objectAtIndex: 进行了多次调用,所以很难知道确切的问题。此外,此代码难以阅读且无法调试。首先按如下方式拆分此代码:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger letterIndex = [pickerLetter selectedRowInComponent:0];
NSString *letter = letters[letterIndex];
NSInteger numberIndex = [pickerNumber selectedRowInComponent:1];
NSString *number = numbers[numberIndex];
NSInteger symbolIndex = [pickerSymbol selectedRowInComponent:2];
NSString *symbol = symbols[symbolIndex];

label.text = [NSString stringWithFormat:@"%@ %@ %@", letter, number, symbol];
}

这样做,您将能够缩小问题的真正原因。

如您所见,问题在于您从 pickerNumberpickerSymbol 引用了错误的组件编号。所有三个选择器都只有 1 个组件,因此您需要从所有三个选择器中选择组件 0

所需的代码是:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger letterIndex = [pickerLetter selectedRowInComponent:0];
NSString *letter = letters[letterIndex];
NSInteger numberIndex = [pickerNumber selectedRowInComponent:0];
NSString *number = numbers[numberIndex];
NSInteger symbolIndex = [pickerSymbol selectedRowInComponent:0];
NSString *symbol = symbols[symbolIndex];

label.text = [NSString stringWithFormat:@"%@ %@ %@", letter, number, symbol];
}

避免在一行代码中调用多个方法。如果出现问题,它会降低代码的可读性和调试难度。

关于ios - pickerViews 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35304199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com