我正在从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(nonatomic)BOOL aBoolVar;
//synthesized etc....

在plist中,该属性被列为Boolen,如果我使用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)

最佳答案

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];

10-04 10:17