Reputation: 413
I try to find item in array by index
here my code.
- (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;
}
I call my method
enumerateIndex = 0;
NSDictionary *comment = [self getItemAtIntex:indexPath.row inArray:comments];
for example, with index 1, in debug I have two answers
subGL 1 -> 1
global 2
I need in response only subGL 1 -> 1
here my file https://www.dropbox.com/s/mvc21a8pl5n6xz3/commenst.json
Upvotes: 0
Views: 306
Reputation: 318934
You aren't doing anything with the return value of the recursive call. Try this:
- (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;
}
Upvotes: 1