在 plist 中处理持久布尔值的最佳方法?
处理从 UISwitch 设置派生并存储在 NSMutableDictionary 中(作为持久设置保存到用户目录中)的布尔值的最佳方法是什么?具体来说,在文件系统中写入和读取的 NSMutableDictionary 中保持布尔值与数值不同的最佳方法是什么?
========
补充:感谢阿德里安和本。因此,结果是,如果通过 [NSNumber numberWithBool:] 存储(这就是我一直在做的),则没有方便的方法来确定该值是否源自布尔值,对吗?
第二部分是我存储的 NSMutableDictionary 最初是通过具有布尔值的 plist 创建的。第一次读入时,这些值具有 NSCFBoolean 类。现在我的策略是将字典中正在使用的值与 plist 中的相应条目进行比较。
这很重要的原因是我正在动态生成界面元素以允许用户操作这些值,并且我将布尔值与 UISwitch 相关联。如果我通过转换为数字而丢失了原始类,则会得到不合适的用户界面元素。
我正在考虑用数组代替存储值,其中第一个值是存储值,其余条目是选择:如果存在两个选择,则将其视为布尔值...在其他情况下,它们用作选择器价值观。但这似乎很麻烦,即使可靠。
任何想法欢迎...
What's the best way to handle Boolean values that derive from a UISwitch setting, and are stored in an NSMutableDictionary that is saved to the user's directory as persistent settings? Specifically, what's the best way to keep boolean values distinct from numeric values in an NSMutableDictionary that gets written to and read from the file system?
========
added: Thanks to Adrian and Ben. So, the upshot is that if stored via [NSNumber numberWithBool:] (this is what I have been doing) there is no convenient way to determine if that value originated as a boolean value, correct?
The second part of this is that the NSMutableDictionary that I'm storing is originally created by creating it via a plist that has Boolean values. These values have the class NSCFBoolean when first read in. Right now my strategy is to compare the value I'm working with in the dictionary with the corresponding entry in the plist.
The reason this matters is that I'm generating interface elements on the fly to allow the user to manipulate these values, and I'm associating booleans with a UISwitch. If I lose the original class by converting to a number, I get an inappropriate user interface element.
I'm considering substituting arrays as the stored value, where the first value is the stored value, and the remaining entries are the choices: where two choices exist, it is treated as a boolean... in other cases, they serve as picker values. But this seems cumbersome, if reliable.
Any ideas welcome...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BOOL 值被序列化为 NSNumber 实例,通过 NSNumber 的 numberWithBool: 静态方法创建:
没有其他方法可以序列化 BOOL 值。在生成的 plist 中,如果保存为 XML,BOOL 值将显示为和<假/>>实体。
(版本,2010 年 1 月 17 日)
根据我上面的评论,这段代码可以帮助识别 NSNumber 实例在内存中加载时的不同子类型,使用“objCType”消息:
其中 untitled.plist 具有以下结构:
在这种情况下,上面代码的执行给出了这样的结果:
也许有一种不同的方法来获取存储在 NSNumber 中的数据的内部类型,但这似乎可以解决问题。
BOOL values are serialized as NSNumber instances, created through NSNumber's numberWithBool: static method:
There is no other way to serialize BOOL values. In the plist that gets generated, if saved as XML, BOOL values appear as <true/> and <false/> entities.
(Edition, Jan 17 2010)
Following my comment above, this snippet of code can help identify different subtypes of NSNumber instances when they are loaded in memory, using the "objCType" message:
Where untitled.plist has this structure:
In this case, the execution of the above code gives this:
Maybe there's a different way to get the internal type of the data stored in NSNumber, but this seems to do the trick.
您还可以使用常量 kCFBooleanTrue 和 kCFBooleanFalse,不过要将它们添加到 NS 中,您需要将它们转换为 ID:
You can also use the constants kCFBooleanTrue and kCFBooleanFalse, though to add these to an NS-anything you'll need to cast them as IDs:
CFBoolean 对象用于表示 CoreFoundation plist 和集合中的布尔值。使用 CFBoolean 可确保最大的兼容性,尤其是在与第三方软件通信时。
以下函数将 CFBoolean 对象的值返回为 C 类型布尔值。
以下常量提供对 true 和 false 对象的访问。
CFBoolean objects are used to represent boolean values in CoreFoundation plists and collections. Using CFBoolean ensures maximum compatibility, especially when communicating with third party software.
The following function returns the value of a CFBoolean object as a C type boolean.
The following constants provide access to the true and false objects.