0

我一直在努力找出这里出了什么问题,由于某种原因JSONKit没有给我我需要的字典,所以我可以在 plist 中引用特定的键/值对。

相反,它显示为 aNSCFString显然不符合 ObjectForKey:. 我四处寻找解决方案;告诉我禁用 ARC、重新启动/重新安装 xcode 以及各种不同的实现,但没有让步。更糟糕的是,我在另一个具有相同功能的项目中实际上拥有相同的代码块,并且可以无缝运行。

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);
    }
}

给出的错误:

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

有问题的plist:

<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>
4

1 回答 1

1

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

于 2013-05-13T15:05:52.107 回答