erik
erik

Reputation: 4948

iterating through an NSDictionary with sub objects

I have a method that takes an 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;
}

any clue as to why the line with the comment under it is throwing an exception: ** First throw call stack:

(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) 

The First NSLog showing the whole NSDictionary shows this:

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

I am trying to create a new object for each node in "filters" getting the id, name, and sequence values

Upvotes: 0

Views: 221

Answers (1)

Dave DeLong
Dave DeLong

Reputation: 243146

Without seeing what your NSLog statement is saying or knowing what your exception actually is, here all the things that might throw an exception in your code:

for(NSString *key in dataDictionary) {

This line could throw if dataDictionary isn't actually an NSDictionary, but were some other non-<NSFastEnumeration> JSON object, like an NSString, NSNumber, etc.

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

This line could throw if key isn't actually an NSString and therefore doesn't implement the method -isEqualToString:.

        NSDictionary *filtersSubDict = [dataDictionary objectForKey:key];

This line could throw if dataDictionary is a fast-enumerable JSON object, but isn't actually an NSDictionary. (In other words, it might be an NSArray, and this line would throw a "does not recognize selector 'objectForKey:'" exception).

        for (NSString *sfKey in filtersSubDict) {

Like above, this could throw if filtersSubDict isn't an NSArray or an NSDictionary.

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

This could throw if filtersSubDict isn't an NSDictionary but is instead an NSArray (same as above).

        }
    }
}

So if it's throwing on the filterObject = ... line, then chances are that filtersSubDict isn't actually a dictionary, and you're getting an unrecognized selector exception.

Upvotes: 4

Related Questions