2014-12-04 86 views
0

我想按字母順序排列uitableview,也可以按章節排序。所以我用這個功能NSDictionary中的奇怪鍵

-(void)updateName{ 
    sectionsNaam = [[NSMutableDictionary alloc] init]; ///Global Object 

     BOOL found; 

     for (Relation *tempRelation in resultsRelations) 
     { 
      NSString *c; 
      if(tempRelation.rel_name.length == 0){ 
       continue; 
      }else{ 
       c = [NSString stringWithFormat:@"%@",[[tempRelation.rel_name substringToIndex:1] uppercaseString]]; 

      } 

      found = NO; 

      for (NSString *str in [sectionsNaam allKeys]) 
      { 
       if ([str isEqualToString:c]) 
       { 
        found = YES; 
       } 
      } 
      NSLog(@"STRIG IS %@",c); 

      if (!found) 
      { 
       [sectionsNaam setValue:[[NSMutableArray alloc] init] forKey:c]; 
      } 
     } 
     for (Relation *tempRelation in resultsRelations) 
     { 
      if(tempRelation.rel_name.length == 0){ 
       continue; 
      }else{ 
       [[sectionsNaam objectForKey:[[tempRelation.rel_name substringToIndex:1] uppercaseString]] addObject:tempRelation]; 
      } 
     } 
    NSLog(@"SECTIONS NAAM IS %@",[sectionsNaam allKeys]); 


    _typeView = 100; 
    [tableRelations reloadData]; 
} 

當我記錄所有的第一個字母,我注意到這些的:

2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS ' 
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS 
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS 
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS " 
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS & 
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS @ 
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS @ 
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS @ 
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS \ 
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS ´ 
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS 「 
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS 「 
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS £ 

,然後當我登錄了NSDictionary中的按鍵我看到這一點:

SECTIONS NAAM IS (
    3, 
    K, 
    4, 
    L, 
    5, 
    M, 
    N, 
    "\U00dc", 
    "\U201c", 
    7, 
    " ", 
    8, 
    O, 
    P, 
    "\U00c7", 
    Q, 
    "\"", 
    R, 
    "\U00c9", 
    S, 
    T, 
    U, 
    "&", 
    "\U00b4", 
    V, 
    "'", 
    W, 
    "(", 
    "@", 
    X, 
    A, 
    Y, 
    B, 
    Z, 
    C, 
    D, 
    "\\", 
    "\U00a3", 
    E, 
    F, 
    "\U00d4", 
    G, 
    H, 
    "\U00d6", 
    1, 
    I, 
    2, 
    J 
) 

現在,當我重新載入我的表時,應用程序在此行上崩潰

rows = [[sectionsNaam valueForKey:[[[sectionsNaam allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count]; 

有了這個錯誤:

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

任何人可以幫助我嗎?提前致謝 !

回答

0

valueForKeyobjectForKey之間的差異。

valueForKey是KVC方法的任何對象找到與您在通過了名稱的屬性。

objectForKey是找到字符串作爲其密鑰對的字典內的對象字典的方法。

更多信息:Difference between objectForKey and valueForKey?

您應該使用objectForKey在您的代碼崩潰。如果找不到,objectForKey可以返回nilvalueForKey要求密鑰存在於課堂中......否則會崩潰。

+0

謝謝它的作品! – Steaphann 2014-12-04 09:51:34

+0

@StefGeelen沒有概率,很高興它有幫助 – 2014-12-04 09:54:49