开发者

Displaying a score from highest to lowest

开发者 https://www.devze.com 2023-03-11 08:44 出处:网络
My score is displaying just not in the correct order. I\'ve been trying to make a method that uses @selector(compare:) but have had no such luck.

My score is displaying just not in the correct order. I've been trying to make a method that uses @selector(compare:) but have had no such luck.

Here's the code I'm working with and I'm wanting to display it from highest to lowest. I'm also wanting to have it so that if you load the app for the first time it creates an empty array so that if the user tries to look at the highscores it doesn't crash the app.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *scoresListPath = [documentsDirectory stringByAppendingPathComponent:@"scores.plist"];

scoresList = 开发者_JAVA百科[[NSMutableArray arrayWithContentsOfFile:scoresListPath] retain];

if (scoresList == nil) {
    scoresList = [[NSMutableArray array] retain];
}

and

- (void)addHighScore:(float)finalScore {
    [scoresList addObject:[NSNumber numberWithFloat:finalScore]];

    [scoresList sortUsingSelector:@selector(compare:)];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *scoresListPath = [documentsDirectory stringByAppendingPathComponent:@"scores.plist"];
    [scoresList writeToFile:scoresListPath atomically:YES];
}


The reason your code is failing is because arrayWithContentsOfFile: returns an immutable array even though it is called on NSMutableArray. You should make a mutable copy of the array that you read of the file like this,

scoresList = [[NSArray arrayWithContentsOfFile:scoresListPath] mutableCopy];

This will give you an NSMutableArray object which can be added to.


Not sure about lowest but,you can use GKLeaderBoardViewController class for counting Highest Score.For more details I suggested you to read below link.Thanks

http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKLeaderboardViewController_Ref/Reference/Reference.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号