gpt4 book ai didi

Objective-C:索引越界

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:06 24 4
gpt4 key购买 nike

我很难弄清楚我的代码到底有什么错误。它说:

reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

这是应用程序崩溃的部分。

NSString *keyTemp = [[NSString alloc] initWithFormat:@"Wit%d",indexPath.section+1];
NSArray *arrTemp;
if ([userStandards objectForKey:keyTemp] == nil || [[userStandards objectForKey:keyTemp] count]==3) {
if ([witDict objectForKey:keyTemp] == nil) {
arrTemp = [[NSArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",nil];
} else {
arrTemp = [[NSArray alloc] initWithArray:[witDict objectForKey:keyTemp]];
}
cell.inputTextArea.text = [NSString stringWithFormat:@"Name: %@\nPhone: %@\nEmail: %@\nCity/State: %@\nZip: %@\nComments: %@",[arrTemp objectAtIndex:0],[arrTemp objectAtIndex:1],[arrTemp objectAtIndex:2], [arrTemp objectAtIndex:3], [arrTemp objectAtIndex:4], [arrTemp objectAtIndex:5]];
} else {
arrTemp = [[NSArray alloc] initWithArray:[userStandards objectForKey:keyTemp]];
cell.inputTextArea.text = [NSString stringWithFormat:@"Name: %@\nPhone: %@\nEmail: %@\nCity/State: %@\nZip: %@\nComments: %@",[arrTemp objectAtIndex:0],[arrTemp objectAtIndex:1],[arrTemp objectAtIndex:2], [arrTemp objectAtIndex:3], [arrTemp objectAtIndex:4], [arrTemp objectAtIndex:5]];
}

最佳答案

检查你的代码:

if ([[userStandards objectForKey:keyTemp] count]==3)
{

// Your array only have 3 elements (index 0,1, and 2)

// Here you accessing index beyond 2
cell.inputTextArea.text = [NSString stringWithFormat:@"Name: %@\nPhone: %@\nEmail: %@\nCity/State: %@\nZip: %@\nComments: %@",[arrTemp objectAtIndex:0],[arrTemp objectAtIndex:1],[arrTemp objectAtIndex:2], [arrTemp objectAtIndex:3], [arrTemp objectAtIndex:4], [arrTemp objectAtIndex:5]];
}

关于Objective-C:索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946928/

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