简体   繁体   English

在NSDictionary中解析NSDictionary的异常

[英]Exception parsing NSDictionary within NSDictionary

I have following plist: 我有以下plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>1_NARR</key>
    <dict>
        <key>audio</key>
        <string>1 NARR AUDIO LOCATION</string>
        <key>text</key>
        <string>1 NARR TEXT</string>
    </dict>
    <key>1_C1_1</key>
    <dict>
        <key>text</key>
        <string>1 C1 1 TEXT</string>
        <key>audio</key>
        <string>1 C1 1 AUDIO LOCATION</string>
        <key>position</key>
        <string>1 C1 1 Position</string>
        <key>frameX</key>
        <string>1 C1 1 FRAMEX</string>
    </dict>
</dict>
</plist>

Here is my code to iterate through the list: 这是我遍历列表的代码:

NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dialogues" ofType:@"plist"]];

    if (mainDictionary != nil){
        NSLog(@"viewDidLoad: mainDictionary != nil");
        [self parseDictionary:mainDictionary];
    }

- (void)parseDictionary:(NSDictionary*)aDictionary
{
    // enumerate key/values, recursing as necessary
    [aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        if ([value isKindOfClass: [NSDictionary class]]) {
            NSLog(@"parseDictionary: isKindOfClass: NSDictionary");
            [self parseDictionary: value];
        } else {
            //... parse value here ...
            NSLog(@"parseDictionary: else: ");
            if ([value valueForKey:@"text"]) 
            {            
                NSLog(@"parseDictionary: else: if: ");
                //NSLog(@"parseDictionary: if: valueForKey:text == %@", [value valueForKey:@"text"]);
            }
        }
    }];
}

I am getting following exception: 我得到以下异常:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x6899760> valueForUndefinedKey:]: this class is not key value coding-compliant for the key text.'

I am unable to figure out why? 我无法弄清楚为什么? Do you see any issues with the logic? 你看到逻辑有什么问题吗? Would appreciate your responses. 非常感谢您的回复。

Here is complete stack trace: 这是完整的堆栈跟踪:

Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<__NSCFString 0x6e7a780> valueForUndefinedKey:]: this class
is not key value coding-compliant for the key text.'
*** First throw call stack: (0x16c0022 0x14e0cd6 0x16bfee1 0x1e7efe 0x156831 0x155c99 0x28bf 0x16bf8f8 0x15e39ed 0x165bbd6 0x165b84d
0x165b795 0x27cb 0x287e 0x16bf8f8 0x15e39ed 0x165bbd6 0x165b84d
0x165b795 0x27cb 0x2a41 0x55fa1e 0x23f1 0x4964be 0x497274 0x4a6183
0x4a6c38 0x49a634 0x1be3ef5 0x1694195 0x15f8ff2 0x15f78da 0x15f6d84
0x15f6c9b 0x496c65 0x498626 0x201d 0x1f95) terminate called throwing
an exception

Consider this section of your plist: 考虑你的plist的这一部分:

  <key>audio</key> <string>1 NARR AUDIO LOCATION</string> 

In your block, key will be the string "audio", and value will be the string "1 NARR AUDIO LOCATION". 在您的块中, key将是字符串“audio”, value将是字符串“1 NARR AUDIO LOCATION”。 The value is a string, and not a dictionary, so we hit the else clause in your condition. 值是一个字符串,而不是字典,所以我们在你的条件中点击了else子句。 You then do this: 然后你这样做:

  if ([value valueForKey:@"text"]) 

but, as we already found, value is a string. 但是,正如我们已经发现的那样, value是一个字符串。 You can't call dictionary methods on a string. 您不能在字符串上调用字典方法。

You are attempting to use the key-value coding API method valueForKey: ( reference ) on an NSString object. 您正在尝试在NSString对象上使用键值编码API方法valueForKey: reference )。

You appear to have your logic back-to-front as I think you are trying to access the text key within an NSDictionary and yet you are performing it when the value is not an NSDictionary instance (ie in the else part of your test). 您似乎已经将逻辑从前到前,因为我认为您正在尝试访问NSDictionarytext键,但是当值不是 NSDictionary实例时(即在测试的else部分中),您正在执行它。

Note: Use [NSDictionary objectForKey:] instead anyway. 注意:无论如何,请使用[NSDictionary objectForKey:]

EDIT : Try this version: 编辑 :试试这个版本:

- (void)parseDictionary:(NSDictionary*)aDictionary
{
    // enumerate key/values, recursing as necessary
    [aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        if ([value isKindOfClass: [NSDictionary class]]) {
            NSLog(@"parseDictionary: isKindOfClass: NSDictionary");
            [self parseDictionary: value];
        } else {
            NSLog(@"'%@'='%@'", key, value);
        }
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM