带时间浮点的NSUserDefaults [英] NSUserDefaults with time floats
问题描述
$ b $ pre $
float BestTimeValue
float starttime
- (void)start {
starttime + = 0.001;
timelabel.text = [NSString stringWithFormat:@%。3f,starttime]; (starttime< BestTimeValue){
[[NSUserDefaults standardUserDefaults] setObject:starttime forKey:[@BestTimeValue];
}
。
}
和在我的viewdidload中:
BestTimeValue = [[NSUserDefaults standardUserDefaults] objectForKey:@BestTimeSaved];
开始时间在游戏结束时停止,但值不会显示在标签中。谢谢!
<[NSUserDefaults standardUserDefaults] setObject:forKey:]正如名称所示,它只接受NSString,NSNumber ,NSDictionary等。
所以你必须使用其他方法[[NSUserDefaults standardUserDefaults] setFloat:forKey]或将浮点保存在NSNumber中以使用setObjet方法[NSNumber numberWithFloat:thefloat];
I'm making a game in which the user must complete a task as quickly as possible, and I would like to display the user's best time and update the best time when he or she achieves a new one. I have:
float BestTimeValue
float starttime
-(void)start{
starttime += 0.001;
timelabel.text = [NSString stringWithFormat:@"%.3f", starttime];
}
if (starttime < BestTimeValue) {
[[NSUserDefaults standardUserDefaults] setObject:starttime forKey:[@"BestTimeValue"];
}
and in my viewdidload:
BestTimeValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"BestTimeSaved"];
starttime stops when the game is over, but the value won't display in the label. Thanks!
[[NSUserDefaults standardUserDefaults] setObject: forKey:] As the name says, it only accept object like NSString, NSNumber, NSDictionary, etc.
So you have to use other method [[NSUserDefaults standardUserDefaults] setFloat: forKey] or save the float in a NSNumber to use the setObjet Method [NSNumber numberWithFloat:thefloat];
这篇关于带时间浮点的NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!