integerForKey:始终返回零
我正在尝试从我的首选项文件中检索一个整数值。有趣的是,它总是返回零,尽管它确实存在,并且 integerForKey:
和 setIntegerForKey:
的名称相同。
以下是我检索值的方法: [defaults integerForKey:@"BackUpSize"];
在这里我设置它: [defaults setInteger:[sizeSlider doubleValue]*8589934592 forKey:@"BackUpSize"] ;
我惊呆了。它应该有效...或者我在这里遗漏了什么?
谢谢
编辑:默认值是这样创建的: NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
编辑:我尝试为此问题创建自己的 NSUserDefaults 方法,但它仍然返回零。
-(void)setDouble:(double)dou forKey:(id)key
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:dou] forKey:key];
}
-(double)doubleForKey:(id)key
{
return [[[NSUserDefaults standardUserDefaults] objectForKey:key] doubleValue];
}
i'm trying to retrieve a value from my preference file which is an integer. The funny thing is that it always returns zero, although it does exist and the names for both integerForKey:
and setIntegerForKey:
are the same.
Here's how I retrieve the value: [defaults integerForKey:@"BackUpSize"];
And here I set it: [defaults setInteger:[sizeSlider doubleValue]*8589934592 forKey:@"BackUpSize"];
I'm stunned. It should work...or am I missing something here?
Thanks
Edit: defaults is created as such: NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
Edit: I've tried creating my own NSUserDefaults method for this issue, but it still returns zero.
-(void)setDouble:(double)dou forKey:(id)key
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:dou] forKey:key];
}
-(double)doubleForKey:(id)key
{
return [[[NSUserDefaults standardUserDefaults] objectForKey:key] doubleValue];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
8589934592
是硬编码文字吗? 真的吗?你至少应该输入类似1 << 33。即便如此,仍将其声明为常数,因为大声喊叫。
如果您正在构建 32 位,则
NSInteger
是一个 32 位有符号值。所以你很可能会溢出。我不确定在这种情况下 NSUserDefaults 的行为会是什么,但值得考虑。8589934592
is a hard-coded literal? Really? You should at least put something like1 << 33
. Even then, declare it as a constant, for crying out loud.If you're building for 32 bit,
NSInteger
is a 32 bit signed value. So you are rather likely to be overflowing. I'm not sure what the behaviour of NSUserDefaults would be in that case, but it's worth considering.此类问题的另一个常见原因是使用将值存储为用户默认值的自定义设置器。如果您这样做,并且如果您不小心只访问用户默认代码中的 ivars,则在读取存储的首选项时,您将面临破坏它们的风险。
Another common cause of this sort of problem is using custom setters that store the values to user defaults. If you do this, and if you aren't careful to access only the ivars in your user defaults code, you run the risk of stomping on the stored prefs while you're reading them.
您正在将双精度值传递给需要整数的内容。通常,您会期望发生一些截断,并最终得到一个 int,但您不能保证
setInteger:forKey:
方法会为您执行此操作。为什么要将滑块值乘以那个明显任意的数字?
编辑:
为了更多地解释我的答案:
为了将数字存储在默认值中,它们需要包装在
NSNumber
对象中。setInteger:forKey
等都是方便的方法,可能只需要您的整数并使用numberWithInt:
从它创建一个NSNnumber
。我不能 100% 确定,因为我还没有看过 Apple 的代码,但我假设将 double 传递给numberWithInt:
将返回 nil,因为 double 不是 int。You're passing a double into something that expects an integer. Normally you'd expect some truncation to occur, and to end up with an int, but you can't guarantee that the
setInteger:forKey:
method is doing that for you.Why are you multiplying your slider value by that apparently arbitrary number?
Edit:
To explain my answer a little more:
In order to store numbers in the defaults they need to be wrapped in
NSNumber
objects.setInteger:forKey
, et al, are convenience methods that probably just take your integer and create anNSNnumber
from it, usingnumberWithInt:
. I can't be 100% sure because I haven't seen Apple's code, but I'd assume that passing a double tonumberWithInt:
will return nil because a double is not an int.