显示高分名称(iPhone 应用程序)
我想做的是将一个名字添加到我的分数列表中。截至目前,我有一个名称被保存为 NSString,我的分数被保存为一个整数,稍后我会想要将它们从高到低(分数)排序。下面是我的代码,它将分数作为整数保存到 plist 中,然后在我的高分视图上从 plist 调用该分数并显示它。我想我需要使用 NSDictionary 来保存名称的字符串和分数的整数。这就是我陷入困境的地方,如果有人可以提供帮助,我将不胜感激。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *scoresListPath = [documentsDirectory stringByAppendingPathComponent:@"scores.plist"];
scoresList = [[NSMutableArray arrayWithContentsOfFile:scoresListPath] retain];
if (scoresList == nil) {
scoresList = [[NSMutableArray array] retain];
- (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];
}
What I want to do is add a name to my score list. As of now I have a name being saved as an NSString and I have my scored being saved as an integer and later on I will want to order these from high to low (scores). Below is my code that is saving the score as a integer out to a plist and then on my high score view I'm calling that score from the plist and displaying it. I think I'm going to need to use an NSDictionary to save the string for the name and the integer for the score. This is where I'm stuck and if anyone can help it would be greatly appreciated.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *scoresListPath = [documentsDirectory stringByAppendingPathComponent:@"scores.plist"];
scoresList = [[NSMutableArray arrayWithContentsOfFile:scoresListPath] retain];
if (scoresList == nil) {
scoresList = [[NSMutableArray array] retain];
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的基本想法是正确的,但你所要做的就是保存一个 NSDictionaries 数组,而不是只保存一个 NSNumbers 数组。
因此,保持 NSMutableArray 不变,只需向其中添加分数:
scoreList 仍会正确写出其 plist,因为 NS[Mutable]Array 包含仅包含数字和字符串的字典,并且这些字典知道如何将自己写出到 plist(参见 NSCoder)。
当您读回数组时,-objectAtIndex:i将是一个NSDictionary,您可以使用-objectForKey:@“name”获取名称,并使用-objectForKey:@“finalScore”获取分数。
You have the basic idea correct, but instead of saving just an array of NSNumbers, all you have to do is save an array of NSDictionaries.
So keep your NSMutableArray the way it is, and simply add scores to it:
The scoreList will still write out its plist properly, because the NS[Mutable]Array contains dictionaries which contains only numbers and strings, and these know how to write themselves out to a plist (see NSCoder).
When you read back your array, the -objectAtIndex:i will be an NSDictionary, and you can get the name with -objectForKey:@"name" and the score with -objectForKey:@"finalScore".