2013-04-09 48 views
0

我有需要一個NSDictionary的方法:通過一個NSDictionary iterrating與子對象

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

第一NSLog的顯示整個的NSDictionary示出了該:

{ 
    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,名稱和序列值

+0

該調用堆棧沒有幫助:/請用符號表示它。 – 2013-04-09 17:53:59

+0

嘗試進入斷點選項卡並打開所有例外的斷點。當它拋出你看到的異常時,這將打破調試器。 – thegrinner 2013-04-09 17:54:43

+0

格式化後請查看您的代碼。我不應該得到+10嗎? – 2013-04-09 18:01:28

回答

4

沒有看到您的NSLog聲明在說什麼或知道您的異常實際上是什麼,在你的代碼可能拋出異常:

for(NSString *key in dataDictionary) { 

此行可能如果dataDictionary實際上不是一個NSDictionary扔,但被其他一些非<NSFastEnumeration> JSON對象,像一個NSStringNSNumber

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

此行可能會引發如果key並不是真正的NSString,因此沒有實現方法-isEqualToString:

 NSDictionary *filtersSubDict = [dataDictionary objectForKey:key]; 

如果dataDictionary是一個快速枚舉JSON對象,但並不是真正的NSDictionary這條線可以拋出。 (換句話說,它可能是NSArray,這一行會拋出一個「無法識別選擇器」objectForKey:「異常)。

 for (NSString *sfKey in filtersSubDict) { 

和上面一樣,這可能拋出,如果filtersSubDict不是NSArrayNSDictionary

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

這可能拋出,如果filtersSubDict不是NSDictionary而是代替NSArray(同上)。

 } 
    } 
} 

所以,如果它拋出的filterObject = ...線,然後有機會,filtersSubDict實際上不是一個字典,你得到一個無法識別的選擇例外。

+1

我喜歡upvoter,他在15秒內閱讀完整的答案! – 2013-04-09 18:07:01