[英]How to load data from NSUserdefaults?
How do I check if there is already data stored in the NSUserdefaults
? 如何检查
NSUserdefaults
是否已存储数据? right now the code crashes when it is trying to load not existing data. 现在,当尝试加载不存在的数据时,代码崩溃。
I tried: 我试过了:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSString *string = [ud objectForKey:@"scoreStringKey"];
if (string)
{
highScore = string.intValue;
}
else
{
highScore = 0;
}
objectForKey
will return nil if no key found Reference objectForKey
将返回nil,如果没有钥匙上发现参考
intValue is a method not attribute. intValue是方法而不是属性。 Link
链接
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSString *string = [ud objectForKey:@"scoreStringKey"];
if (string != nil)
{
highScore = [string intValue]; <--- right way to convert into integer
}
else
{
highScore = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.