objective-cjsonkitnscfstring

JSONKit returning unwanted NSCFString rather than NSDictionary


I've been pulling my hair out tried to figure out whats wrong here, for some reason JSONKit isn't giving me the dictionary I need so I can reference particular key/value pairs within the plist.

Instead its displaying as a NSCFString which obviously doesn't conform to methods like ObjectForKey:. I've scoured around for solutions; telling me to disable ARC, restart/reinstall xcode, and a variety of different implementations but no budging. Worse yet, I have effectively the same code block in another project with the same function and it works seamlessly.

NSError * error = NULL;
NSData * plistData = [NSData dataWithContentsOfFile:filepath];
id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListImmutable format:NULL error:&error];
NSString * jsonString = [plist JSONStringWithOptions:JKSerializeOptionPretty error:&error];
NSDictionary * returnDictionary = [jsonString objectFromJSONString];
for(id elem in returnDictionary)
{
    for(id elements in elem)
    {
        NSLog(@"%@",elements);
    }
}

The error given:

-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1815750

The plist in question:

<dict>
    <key>20003</key>
    <dict>
        <key>type</key>
        <string>1</string>
        <key>name</key>
        <string>Home Name</string>
        <key>font</key>
        <string>Courier</string>
        <key>size</key>
        <string>22</string>
        <key>color</key>
        <string>FFFFFFFF</string>
    </dict>
    <key>20001</key>
    <dict>
        <key>type</key>
        <string>1</string>
        <key>name</key>
        <string>heyhey</string>
        <key>font</key>
        <string>XXX</string>
        <key>size</key>
        <string>11</string>
        <key>color</key>
        <string>FFFF0000</string>
    </dict>
</dict>
</plist>

Solution

  • Problem is not JSONKit not returning NSDictionary.

    Problem is that when you enumerate through a NSDictionary, you get the "key", not the "value".

    So, for the following codes:

    for(id elem in returnDictionary)
    {
        for(id elements in elem)
        {
            NSLog(@"%@",elements);
        }
    }
    

    The type of elem in the outer loop is the "key" for each entry in the dictionary. (Which, from your plist, is a string)

    Change it to

    for(id elem in returnDictionary)
    {
        id val = returnDictionary[ elem ];
        for(id elements in val)
        {
            NSLog(@"%@",elements);
        }
    }
    

    See if that helps