NSUserDefaults 和原始数据类型?
当 -registerDefaults: 仅接受可以不包含任何原始数据类型 - 仅包含对象。
相反,如果我希望能够为浮点数提供任何实际的默认值,我似乎必须使用 -setObject:forKey 和 -objectForKey: 并仅存储 NSNumber 对象。
我在这里缺少什么?
What is the point of the NSUserDefaults methods such as -setFloat:forKey: and -floatForKey: when -registerDefaults: accepts only a NSDictionary which can't hold any primitive data types - only objects.
Instead it seems I have to use -setObject:forKey and -objectForKey: and store only NSNumber objects if I want to be able to give my floats any actual default values.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setFloat:
只是一个方便的方法,它创建一个 NSNumber,然后将其传递给setObject:
。floatForKey:
则相反。NSDictionary
只能保存对象类型,因此您需要将基元包装在适当的对象中。所以是的,您确实需要执行您正在执行的操作来设置默认值。如果您可以直接在 NSDictionary 上使用这些方法,那就太好了,这将是一个非常简单的类别。
setFloat:
is just a convenience method that creates an NSNumber and then passes that tosetObject:
.floatForKey:
does the reverse.NSDictionary
can only hold object types, so you need to wrap primitives in the appropriate objects. So yes, you do need to do what you are doing to set up the default defaults.It would be nice if you could use those methods directly on an NSDictionary, that would be a pretty trivial category to write.