索引

2013-12-12 74 views
0

的NSArray遞歸搜索我試圖通過索引索引

http://content.screencast.com/users/xdozorx/folders/Jing/media/cb5e24cf-9349-4aa2-a886-bfafb96299f5/00000051.png

這裏我的代碼找到數組項。

- (NSDictionary *) getItemAtIntex:(int) index inArray:(NSArray *) array 
{ 
    for (NSDictionary *item in array) 
    { 
     if (enumerateIndex == index) 
     { 
      NSLog(@"%@",item[@"comment"]); 
      return item; 
     } 
     else if ([item[@"childs"] count]) 
     { 
      enumerateIndex ++; 
      [self getItemAtIntex:index inArray:item[@"childs"]]; 
     } 
    } 
    return nil; 
} 

我打電話給我的方法

enumerateIndex = 0; 
NSDictionary *comment = [self getItemAtIntex:indexPath.row inArray:comments]; 

例如,索引1,在調試我有兩個答案

subGL 1 -> 1 
global 2 

我需要在響應僅subGL 1 -> 1

這裏我文件https://www.dropbox.com/s/mvc21a8pl5n6xz3/commenst.json

回答

1

你沒有對遞歸調用的返回值做任何事情。試試這個:

- (NSDictionary *) getItemAtIntex:(int) index inArray:(NSArray *) array 
{ 
    for (NSDictionary *item in array) 
    { 
     if (enumerateIndex == index) 
     { 
      NSLog(@"%@",item[@"comment"]); 
      return item; 
     } 
     else if ([item[@"childs"] count]) 
     { 
      enumerateIndex ++; 
      NSDictionary *result = [self getItemAtIntex:index inArray:item[@"childs"]]; 
      if (result) { 
       return result; 
      } 
     } 
    } 
    return nil; 
}