Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolForKey unrecognized selector error iOS

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)
like image 473
M Jesse Avatar asked May 12 '26 19:05

M Jesse


1 Answers

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];
like image 115
UIAdam Avatar answered May 14 '26 10:05

UIAdam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!