Objective c 尝试对数组排序时崩溃
我正试图在我制作的一款太空射击游戏中获得高分。因此,我要做的是使用以下代码保存分数:Objective c 尝试对数组排序时崩溃,objective-c,sorting,crash,nsmutablearray,runtime-error,Objective C,Sorting,Crash,Nsmutablearray,Runtime Error,我正试图在我制作的一款太空射击游戏中获得高分。因此,我要做的是使用以下代码保存分数: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSNumber *highscore = [[NSNumber alloc] initWithInt:_
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSNumber *highscore = [[NSNumber alloc] initWithInt:_totalSeconds];
NSString *choice = [NSString stringWithFormat:@"%@/userschoice", documentsDirectory];
NSMutableArray *array = [NSArray arrayWithContentsOfFile:choice];
[array addObject: highscore];
[array writeToFile:choice atomically:YES];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *choice = [NSString stringWithFormat:@"%@/userschoice", documentsDirectory];
NSMutableArray *array = [NSArray arrayWithContentsOfFile:choice];
int _highscoresAmount = [array count];
NSNumber *highscore2 = [[NSNumber alloc] init];
NSNumber *highscore2Two = [[NSNumber alloc] init];
int _count = 0;
while (_count<_highscoresAmount){
for (int i=0; i<_highscoresAmount; i++){
int j = i+1;
highscore2 = [array objectAtIndex:i];
highscore2Two = [array objectAtIndex:j];
if (highscore2Two > highscore2){
[array replaceObjectAtIndex:i withObject:highscore2Two];
[array replaceObjectAtIndex:j withObject:highscore2];
}
}
_count = _count + 1;
}
for (int i=0; i< _highscoresAmount;i++){
highscore2 = [array objectAtIndex:i];
NSString *highscore3 = [NSString stringWithFormat:@"%@", highscore2];
NSLog(highscore3);
}
其中整数\u totalSeconds
是用户的分数。然后,在我的viewDidLoad
函数中,我输入以下代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSNumber *highscore = [[NSNumber alloc] initWithInt:_totalSeconds];
NSString *choice = [NSString stringWithFormat:@"%@/userschoice", documentsDirectory];
NSMutableArray *array = [NSArray arrayWithContentsOfFile:choice];
[array addObject: highscore];
[array writeToFile:choice atomically:YES];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *choice = [NSString stringWithFormat:@"%@/userschoice", documentsDirectory];
NSMutableArray *array = [NSArray arrayWithContentsOfFile:choice];
int _highscoresAmount = [array count];
NSNumber *highscore2 = [[NSNumber alloc] init];
NSNumber *highscore2Two = [[NSNumber alloc] init];
int _count = 0;
while (_count<_highscoresAmount){
for (int i=0; i<_highscoresAmount; i++){
int j = i+1;
highscore2 = [array objectAtIndex:i];
highscore2Two = [array objectAtIndex:j];
if (highscore2Two > highscore2){
[array replaceObjectAtIndex:i withObject:highscore2Two];
[array replaceObjectAtIndex:j withObject:highscore2];
}
}
_count = _count + 1;
}
for (int i=0; i< _highscoresAmount;i++){
highscore2 = [array objectAtIndex:i];
NSString *highscore3 = [NSString stringWithFormat:@"%@", highscore2];
NSLog(highscore3);
}
NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*choice=[NSString stringWithFormat:@“%@/userschoice”,documentsDirectory];
NSMUTABLEARRY*array=[NSArray ARRAYWITHCONTENTSOFILE:choice];
int_highscoresAmount=[数组计数];
NSNumber*高分2=[[NSNumber alloc]init];
NSNumber*highscore2Two=[[NSNumber alloc]init];
int _计数=0;
while(_count我发现这里有两个主要错误。第一个错误是,您将数组声明为NSMutableArray,但实际上创建了一个NSArray。您需要使用[NSMutableArray arrayWithContentsOfFile:]创建一个可变数组
第二个问题是,您不应该实现自己的排序算法,只需使用内置的排序方法(-sortUsingComparator:例如)
(编辑)
哦,另外,你应该使用-stringByAppendingPathComponent而不是+stringWithFormat来构建路径我解决了nsmutable数组的第一个问题。该应用程序仍然无法运行。我尝试了stringByAppendingPathComponent,但它向我发出警告,NSString可能不会响应stringByAppendingPathComponent。请继续nd我使用的是xcode 3,因此可能会导致一些问题。您应该发布崩溃日志。在没有崩溃日志的情况下调试崩溃基本上是不可能的。我添加了崩溃日志。另外,可能是objectAtIndex:…没有收到int变量吗?那不是崩溃日志。这只是来自调试器的一堆错误消息。