简体   繁体   English

NSUserDefaults和原始数据类型?

[英]NSUserDefaults and primitive data types?

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. 是什么的方法NSUserDefaults的诸如-setFloat:forKey:-floatForKey:-registerDefaults:只接受的NSDictionary也不能容纳任何原始数据类型-唯一对象。

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. 相反,我似乎必须使用-setObject:forKey-objectForKey:并且只存储NSNumber对象,如果我想能够为我的浮点数赋予任何实际的默认值。

What am I missing here? 我在这里想念什么?

setFloat: is just a convenience method that creates an NSNumber and then passes that to setObject: . setFloat:只是一种方便的方法,它创建一个NSNumber,然后将其传递给setObject: floatForKey: does the reverse. floatForKey:执行相反的操作。

NSDictionary can only hold object types, so you need to wrap primitives in the appropriate objects. NSDictionary只能保存对象类型,因此您需要将原语包装在适当的对象中。 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. 如果您可以直接在NSDictionary上使用这些方法,那就太好了,这将是一个非常琐碎的类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM