[ios]通过与子对象 NSDictionary iterrating

发布时间: 2017/3/21 23:03:08
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个需要 NSDictionary 的方法︰

-(id)initWithJSONDictionary:(NSDictionary *)dataDictionary{
     self = [super init];
     NSLog(@"********************************* %@ ",dataDictionary);
     for(NSString * key in dataDictionary){
         if([key isEqualToString:@"filters"]){
             NSDictionary * filtersSubDict = [dataDictionary objectForKey:key];
             for(NSString *sfKey in filtersSubDict){
                 NSLog(@"new filter: %@", sfKey );               
                 NSDictionary *filterObject = [filtersSubDict objectForKey:sfKey];
                 // this line is throwing some kind of thread exception
             }
          }
      }
      return self;
}

为什么在它之下的评论线抛出异常的任何线索: * * 第一次扔调用堆栈︰

(0x1d04012 0x1141e7e 0x1d8f4bd 0x1cf3bbc 0x1cf394e 0x1052b 0x137fe 0x259b3 0x4a6853f 0x4a7a014 0x4a6a7d5 0x1caaaf5 0x1ca9f44 0x1ca9e1b 0x1c5e7e3 0x1c5e668 0x85ffc 0x27fd 0x2725)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

显示整个 NSDictionary 第一 NSLog 演示这一情况︰

{
    errorCode = 0;
    filters =     (
                {
            id = 1001;
            name = "Base Lenses";
            sequence = 1;
        },
                {
            id = 1002;
            name = "Standard Anti-Reflective";
            sequence = 2;
        },
                {
            id = 1003;
            name = "Premium Anti-Reflective";
            sequence = 3;
        },
                {
            id = 1004;
            name = "Enhanced Scratch Resistance";
            sequence = 4;
        },
                {
            id = 1005;
            name = Sun;
            sequence = 5;
        },
                {
            id = 1006;
            name = Tint;
            sequence = 6;
        },
                {
            id = 1007;
            name = "Clear To Dark";
            sequence = 7;
        }
    );
    lenses =     {
        Glass =         (
                        {
                fsv = 1;
                inStore = 1;
                lom = 0;
                price = 465;
                style = "Glass Std AR";
                styleFilters =                 (
                    1002
                );
                type = "Single Vision";
                visionCorrection = singleVision;
            },
                        {
                fsv = 1;
                inStore = 0;
                lom = 1;
                price = 395;
                style = "Prem Plastic Std AR";
                styleFilters =                 (
                    1002
                );
                type = "SV HD";
                visionCorrection = singleVision;
            }
        );
        "Plastic/Hi-index" =         (
                        {
                fsv = 1;
                inStore = 1;
                lom = 0;
                price = 395;
                style = "Prem Plastic Std AR";
                styleFilters =                 (
                    1002,
                    1006
                );
                type = "SV HD";
                visionCorrection = singleVision;
            },
                        {
                fsv = 1;
                inStore = 0;
                lom = 1;
                price = 465;
                style = "Glass Std AR";
                styleFilters =                 (
                    1002,
                    1006
                );
                type = "SV HD";
                visionCorrection = singleVision;
            }
        );
        Polycarbonate =         (
                        {
                fsv = 1;
                inStore = 1;
                lom = 0;
                price = 395;
                style = "FeatherWates Classic";
                styleFilters =                 (
                    1001
                );
                type = "SV Wrap";
                visionCorrection = singleVision;
            },
                        {
                fsv = 1;
                inStore = 0;
                lom = 1;
                price = 495;
                style = "FeatherWates Classic";
                styleFilters =                 (
                    1001
                );
                type = "SV Wrap";
                visionCorrection = singleVision;
            }
        );
    };
    materials =     (
        Polycarbonate,
        "Plastic/Hi-index",
        Glass
    );
} 

我想创建一个新对象的每个节点在"筛选器"获取 id、 名称和序列值

解决方法 1:

没有看到什么你 NSLog 语句是说或知道你例外实际上什么,这里所有的东西那可能会引发异常,在您的代码︰

for(NSString *key in dataDictionary) {

这条线可以使用 if dataDictionary 不是实际上 NSDictionary ,但一些其他的非 <NSFastEnumeration> JSON 对象,像 NSStringNSNumber ,等等。

    if([key isEqualToString:@"filters"]){

这条线可以使用 if key 不是实际上 NSString 和因此不实现该方法 -isEqualToString:

        NSDictionary *filtersSubDict = [dataDictionary objectForKey:key];

这条线可以使用 if dataDictionary 是一个快速可枚举的 JSON 对象,但不是实际上 NSDictionary 。 (换句话说,它可能是 NSArray ,和这条线会抛出"不能识别选择器 ' objectForKey:'"例外)。

        for (NSString *sfKey in filtersSubDict) {

像在上面,这可以使用 if filtersSubDict 不是 NSArrayNSDictionary

            NSLog(@"new filter: %@", sfKey );
            NSDictionary *filterObject = [filtersSubDict objectForKey:sfKey];

这可以使用 if filtersSubDict 不是 NSDictionary 而是 NSArray (以上相同)。

        }
    }
}

所以,如果它扔 filterObject = ... 线,然后很有可能 filtersSubDict 不是其实是一本字典,越来越无法识别选择器异常。

官方微信
官方QQ群
31647020