boolForKey 无法识别的选择器错误 iOS

发布于 2025-01-06 15:20:20 字数 1608 浏览 1 评论 0原文

我正在从 plist 文件读入数组。当我迭代数组时,我将每个字符串保存到数据结构的相应部分。当我到达 BOOL 变量并尝试 boolForKey 从数组中提取它时,我收到无法识别的选择器错误。

这是一些代码:

for(int i = 0; i<myArray.count;i++){

[myDataStructure setName:[[myArray objectAtIndex:i] objectForKey:NAMEVAR_KEY]];
//works fine
....
[myDataStructure setABoolVar:[[myArray objectAtIndex:i] boolForKey:BOOLVAR_KEY]];
//crash
.... 
}

数据结构中的属性如下: @property(非原子,强) NSString* 名称; @property(非原子) BOOL aBoolVar;

//synthesized etc....

在 plist 中,该属性被列为布尔值,如果我使用 objectForKey 我会收到警告,但它不会崩溃。

有什么想法吗?

这是 XML

<dict>
<key>Root</key>
<array>
    <dict>
        <key>name</key>
        <string>Bobby</string>
        <key>aBoolValue</key>
        <true/>

    </dict>
....
    </array>
</dict>

....

来自控制台

2012-02-16 23:45:21.866 CSOTest4[1452:f803] -[__NSCFDictionary boolForKey:]: unrecognized selector sent to instance 0x6d296a0
2012-02-16 23:45:21.867 CSOTest4[1452:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary boolForKey:]: unrecognized selector sent to instance 0x6d296a0'
*** First throw call stack:
(0x13c2052 0x1553d0a 0x13c3ced 0x1328f00 0x1328ce2 0x3980 0xdf64e 0xdec1c 0x10556d 0xefd47 0x106441 0x10645d 0x10645d 0x1064f9 0x46d65 0x46dac 0x17be6 0x188a6 0x27743 0x281f8 0x1baa9 0x12acfa9 0x13961c5 0x12fb022 0x12f990a 0x12f8db4 0x12f8ccb 0x182a7 0x19a9b 0x2618 0x2575 0x1)

I am reading from a plist file into an array. When I iterate through the array I save each string it to its respective part of the data structure. When I get to the BOOL variable and try boolForKey to extract it from the array I get an unrecoginized selector error.

Here is some code:

for(int i = 0; i<myArray.count;i++){

[myDataStructure setName:[[myArray objectAtIndex:i] objectForKey:NAMEVAR_KEY]];
//works fine
....
[myDataStructure setABoolVar:[[myArray objectAtIndex:i] boolForKey:BOOLVAR_KEY]];
//crash
.... 
}

The properties in the data structure are like this:
@property(nonatomic, strong) NSString* name;
@property(nonatomic) BOOL aBoolVar;

//synthesized etc....

In the plist, the property is listed as a Boolen, if I use objectForKey I get a warning but it doesn't crash.

Any ideas?

This is the XML

<dict>
<key>Root</key>
<array>
    <dict>
        <key>name</key>
        <string>Bobby</string>
        <key>aBoolValue</key>
        <true/>

    </dict>
....
    </array>
</dict>

....

From the console

2012-02-16 23:45:21.866 CSOTest4[1452:f803] -[__NSCFDictionary boolForKey:]: unrecognized selector sent to instance 0x6d296a0
2012-02-16 23:45:21.867 CSOTest4[1452:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary boolForKey:]: unrecognized selector sent to instance 0x6d296a0'
*** First throw call stack:
(0x13c2052 0x1553d0a 0x13c3ced 0x1328f00 0x1328ce2 0x3980 0xdf64e 0xdec1c 0x10556d 0xefd47 0x106441 0x10645d 0x10645d 0x1064f9 0x46d65 0x46dac 0x17be6 0x188a6 0x27743 0x281f8 0x1baa9 0x12acfa9 0x13961c5 0x12fb022 0x12f990a 0x12f8db4 0x12f8ccb 0x182a7 0x19a9b 0x2618 0x2575 0x1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

丘比特射中我 2025-01-13 15:20:20

NSDictionary 没有名为 boolForKey 的方法。您必须首先使用 objectForKey,这将为您提供一个 NSNumber,然后在 NSNumber 上调用 boolValue

BOOL myBool = [[[myArray objectAtIndex:i] objectForKey:BOOLVAR_KEY] boolValue];

或将其分解为几部分:

NSDictionary myDictionary = [myArray objectAtIndex:i];
NSNumber myNumber = [myDictionary objectForKey:BOOLVAR_KEY];
BOOL myBool = [myNumber boolValue];

NSDictionary does not have a method called boolForKey. You have to use objectForKey first, which will give you an NSNumber, and then call boolValue on the NSNumber.

BOOL myBool = [[[myArray objectAtIndex:i] objectForKey:BOOLVAR_KEY] boolValue];

or to break it down into pieces:

NSDictionary myDictionary = [myArray objectAtIndex:i];
NSNumber myNumber = [myDictionary objectForKey:BOOLVAR_KEY];
BOOL myBool = [myNumber boolValue];
爱格式化 2025-01-13 15:20:20

你的 PLIST 中的布尔值应该是 YES 而不是 True 吗?

这有效

[myDataStructure setABoolVar:([(NSDictionary*) 
        [myArray objectAtIndex:i]valueForKey:BOOLVAR_KEY]== 
                                        [NSNumber numberWithBool:YES]?YES:NO)];

Should your boolean in your PLIST be YES rather than True?

This works

[myDataStructure setABoolVar:([(NSDictionary*) 
        [myArray objectAtIndex:i]valueForKey:BOOLVAR_KEY]== 
                                        [NSNumber numberWithBool:YES]?YES:NO)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文