在 plist 中处理持久布尔值的最佳方法?

发布于 2024-08-16 22:37:27 字数 582 浏览 7 评论 0原文

处理从 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 技术交流群。

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

发布评论

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

评论(3

亚希 2024-08-23 22:37:27

BOOL 值被序列化为 NSNumber 实例,通过 NSNumber 的 numberWithBool: 静态方法创建:

BOOL ok = YES;
NSNumber *value = [NSNumber numberWithBool:ok];

// ...later...

BOOL answer = [value boolValue];

没有其他方法可以序列化 BOOL 值。在生成的 plist 中,如果保存为 XML,BOOL 值将显示为和<假/>>实体。

(版本,2010 年 1 月 17 日)

根据我上面的评论,这段代码可以帮助识别 NSNumber 实例在内存中加载时的不同子类型,使用“objCType”消息:

NSString *path = [[NSBundle mainBundle] pathForResource:@"untitled" 
                                                 ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];

for (id item in array)
{
    NSLog(@"%@", [NSString stringWithCString:[item objCType] 
                                    encoding:NSUTF8StringEncoding]);
}

其中 untitled.plist 具有以下结构:

<plist version="1.0">
<array>
    <integer>123</integer>
    <true/>
    <real>1.2</real>
</array>
</plist>

在这种情况下,上面代码的执行给出了这样的结果:

2010-01-17 17:04:35.100 Untitled[62206:207] q
2010-01-17 17:04:35.101 Untitled[62206:207] c
2010-01-17 17:04:35.102 Untitled[62206:207] d

也许有一种不同的方法来获取存储在 NSNumber 中的数据的内部类型,但这似乎可以解决问题。

BOOL values are serialized as NSNumber instances, created through NSNumber's numberWithBool: static method:

BOOL ok = YES;
NSNumber *value = [NSNumber numberWithBool:ok];

// ...later...

BOOL answer = [value boolValue];

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:

NSString *path = [[NSBundle mainBundle] pathForResource:@"untitled" 
                                                 ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];

for (id item in array)
{
    NSLog(@"%@", [NSString stringWithCString:[item objCType] 
                                    encoding:NSUTF8StringEncoding]);
}

Where untitled.plist has this structure:

<plist version="1.0">
<array>
    <integer>123</integer>
    <true/>
    <real>1.2</real>
</array>
</plist>

In this case, the execution of the above code gives this:

2010-01-17 17:04:35.100 Untitled[62206:207] q
2010-01-17 17:04:35.101 Untitled[62206:207] c
2010-01-17 17:04:35.102 Untitled[62206:207] d

Maybe there's a different way to get the internal type of the data stored in NSNumber, but this seems to do the trick.

原谅我要高飞 2024-08-23 22:37:27

您还可以使用常量 kCFBooleanTrue 和 kCFBooleanFalse,不过要将它们添加到 NS 中,您需要将它们转换为 ID:

[myDictionary setObject: (id) kCFBooleanTrue forKey: @"am_I_awesome"];

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:

[myDictionary setObject: (id) kCFBooleanTrue forKey: @"am_I_awesome"];
短暂陪伴 2024-08-23 22:37:27

CFBoolean 对象用于表示 CoreFoundation plist 和集合中的布尔值。使用 CFBoolean 可确保最大的兼容性,尤其是在与第三方软件通信时。

以下函数将 CFBoolean 对象的值返回为 C 类型布尔值。

Boolean CFBooleanGetValue (CFBooleanRef boolean);

以下常量提供对 true 和 false 对象的访问。

const CFBooleanRef kCFBooleanTrue;
const CFBooleanRef kCFBooleanFalse;

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.

Boolean CFBooleanGetValue (CFBooleanRef boolean);

The following constants provide access to the true and false objects.

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