integerForKey:始终返回零

发布于 2024-09-06 17:14:15 字数 788 浏览 3 评论 0原文

我正在尝试从我的首选项文件中检索一个整数值。有趣的是,它总是返回零,尽管它确实存在,并且 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还如梦归 2024-09-13 17:14:15

8589934592 是硬编码文字吗? 真的吗?你至少应该输入类似 1 << 33。即便如此,仍将其声明为常数,因为大声喊叫。

如果您正在构建 32 位,则 NSInteger 是一个 32 位有符号值。所以你很可能会溢出。我不确定在这种情况下 NSUserDefaults 的行为会是什么,但值得考虑。

8589934592 is a hard-coded literal? Really? You should at least put something like 1 << 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.

甩你一脸翔 2024-09-13 17:14:15

此类问题的另一个常见原因是使用将值存储为用户默认值的自定义设置器。如果您这样做,并且如果您不小心只访问用户默认代码中的 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.

花想c 2024-09-13 17:14:15

您正在将双精度值传递给需要整数的内容。通常,您会期望发生一些截断,并最终得到一个 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 an NSNnumber from it, using numberWithInt:. I can't be 100% sure because I haven't seen Apple's code, but I'd assume that passing a double to numberWithInt: will return nil because a double is not an int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文