gpt4 book ai didi

ios - xcode 中的 SIGABRT 错误 : NSMutableArray fails to load all objects creating null reference

转载 作者:行者123 更新时间:2023-11-29 04:51:58 24 4
gpt4 key购买 nike

我正在尝试创建一个测验应用程序。我的代码应该从 CSV 加载问题和答案,然后随机显示它们。在模拟器上,至少在问题 85 之前它都运行良好。之后它开始抛出 SIGABRT 错误:

2011-12-27 19:03:22.026 100Q_Capitals[5369:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 99 beyond bounds [0 .. 95]'

这是我使用的代码:

questionTotal 和 questionCount100 都初始化为 int,然后为了设置数组并从 CSV 中读取,我写了这个

NSString *pathA = [NSString stringWithFormat:@"%@%@",
[[NSBundle mainBundle] resourcePath],
@"/right.wav"];
NSURL *filePathA = [NSURL fileURLWithPath:pathA isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePathA, &rightSound);

NSMutableArray *newArray1 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray2 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray3 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray4 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray5 = [[NSMutableArray alloc]initWithCapacity:200];

self.questionArray = newArray1;
self.correctArray = newArray2;
self.wrongArray1 = newArray3;
self.wrongArray2 = newArray4;
self.wrongArray3 = newArray5;

[newArray1 release];
[newArray2 release];
[newArray3 release];
[newArray4 release];
[newArray5 release];

NSString* pathCSV = [[NSBundle mainBundle] pathForResource:@"capitals1" ofType:@"csv"];
NSString *fileString = [NSString stringWithContentsOfFile:pathCSV encoding:NSUTF8StringEncoding error:NULL];
if (!fileString) {
NSLog(@"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n,;"]];

NSString *id = nil, *hashOne = nil, *hashTwo = nil, *hashThree = nil, *hashFour = nil, *hashFive=nil;
while ([scanner scanUpToString:@"," intoString:&id] && [scanner scanUpToString:@"," intoString:&hashOne] && [scanner scanUpToString:@"," intoString:&hashTwo] && [scanner scanUpToString:@"," intoString:&hashThree] && [scanner scanUpToString:@"," intoString:&hashFour] && [scanner scanUpToString:@"\n" intoString:&hashFive])
{
// NSLog(@"id:%@, 1:%@, 2:%@, 3:%@:, 4:%@, 5:%@", id, hashOne, hashTwo, hashThree, hashFour, hashFive);

[questionArray addObject:hashOne];
[correctArray addObject:hashTwo];
[wrongArray1 addObject:hashThree];
[wrongArray2 addObject:hashFour];
[wrongArray3 addObject:hashFive];


questionTotal++;
}

appDelegate.saveArrayQ = questionArray;
appDelegate.saveArrayC = correctArray;
appDelegate.saveArray1 = wrongArray1;
appDelegate.saveArray2 = wrongArray2;
appDelegate.saveArray3 = wrongArray3;

然后为了访问该数组并将其显示为测验的一部分,我编写了此内容(第一行 NSString*question ... 行是发生 SIGABRT 错误的位置)。

showQuestion{

for (UIButton *thisButton in [self.view subviews])
{
if ([thisButton isKindOfClass:[UIButton class]])
{
if (thisButton.tag != 100)
{
[thisButton addTarget:self action:@selector(selectAnswer:) forControlEvents:UIControlEventTouchUpInside];
}else{}
}else{}
}

[answerButton1 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
[answerButton2 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
[answerButton3 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
[answerButton4 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];

secondsLeft = appDelegate.timerLength;

NSString *timerString = [[NSString alloc]initWithFormat:@"0:%02d",secondsLeft];
timerLabel.text = timerString;
[timerString release];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateDuration) userInfo:nil repeats:YES];

int q = arc4random() % 100;
q = abs(q);

NSString *num = [[NSString alloc]initWithFormat:@"%d",appDelegate.saveQ100 + 1];
numberLabel.text = num;
[num release];

NSString *question = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArrayQ objectAtIndex:q]];
NSString *choice1 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArrayC objectAtIndex:q]];
NSString *choice2 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArray1 objectAtIndex:q]];
NSString *choice3 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArray2 objectAtIndex:q]];
NSString *choice4 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArray3 objectAtIndex:q]];

等ETC等等

如果有人能在这里帮助我,我将非常感激。我花了两天时间试图找出问题所在。

最佳答案

当您生成一个数字并存储在 q 中时,它将是 0 到 99 之间的数字。但从错误消息来看,您有一个只有 96 个元素的数组。您无法访问不在数组中的元素。我真的不知道你想做什么,但只是尝试生成 0 到 95 之间的数字

int q = arc4random() % 96;

关于ios - xcode 中的 SIGABRT 错误 : NSMutableArray fails to load all objects creating null reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8653575/

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