使用子对象迭代NSDictionary

时间:2013-04-09 17:52:54

标签: ios objective-c nsdictionary

我有一个采用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,name和sequence值

1 个答案:

答案 0 :(得分:4)

在没有看到您的NSLog语句的内容或知道您的异常实际是什么的情况下,可能会在您的代码中抛出异常的所有内容:

for(NSString *key in dataDictionary) {

如果dataDictionary实际上不是NSDictionary,则该行可能会抛出,但是其他非<NSFastEnumeration> JSON对象,如NSString,{{1}等等。

NSNumber

如果 if([key isEqualToString:@"filters"]){ 实际上不是key,则此行可能会抛出,因此未实施方法NSString

-isEqualToString:

如果 NSDictionary *filtersSubDict = [dataDictionary objectForKey:key]; 是一个快速可枚举的JSON对象,但是实际上不是dataDictionary,则该行可能会抛出。 (换句话说,它可能是NSDictionary,这一行会抛出“不识别选择器'objectForKey:'”异常。)

NSArray

如上所述,如果 for (NSString *sfKey in filtersSubDict) { 不是filtersSubDictNSArray,则可能会抛出。

NSDictionary

如果 NSLog(@"new filter: %@", sfKey ); NSDictionary *filterObject = [filtersSubDict objectForKey:sfKey]; 不是filtersSubDict,而是NSDictionary(与上述相同),则可能会抛出。

NSArray

因此,如果它放在 } } } 行上,那么filterObject = ...实际上不是字典,并且你得到一个无法识别的选择器异常。