我仍然对objective-c很新,我正在为我的班级做一个结局项目。我想要做的是从NSUserDefaults读取3个高分的数组并在第一个屏幕上显示它们。
这是我的代码
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *highscores = [[NSArray alloc] initWithArray:[defaults arrayForKey:@"highscores"]];
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",highscores(0),highscores(1),highscores(2); //error on this line
myLabel.fontSize = 30;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:myLabel];
当我尝试传递高分时,我得到的错误是NSArrays不是函数或功能点
答案 0 :(得分:1)
使用此行编辑错误行
myLabel.text = [NSString stringWithFormat:@"High Scores \n 1)%f \n 2)%f \n 3) %f",
[[highscores objectAtIndex:0] floatValue],
[[highscores objectAtIndex:1] floatValue],
[[highscores objectAtIndex:2] floatValue]];
答案 1 :(得分:0)
替换行
myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",highscores(0),highscores(1),highscores(2);
带
myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",[highscores objectAtIndex:0],[highscores objectAtIndex:1],[highscores objectAtIndex:2];
希望这能解决问题。
答案 2 :(得分:-1)
Try bellow code .......
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *highscores = [[NSArray alloc] initWithArray:[defaults arrayForKey:@"highscores"]];
highscores = [highscores sortedArrayUsingComparator:
^NSComparisonResult(id obj1, id obj2) {
if ([obj1 intValue] < [obj2 intValue]) {
return NSOrderedAscending;
} else if ([obj1 intValue] > [obj2 intValue]) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}];
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",highscores(0),highscores(1),highscores(2); //error on this line
myLabel.fontSize = 30;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:myLabel];