Pavel
Pavel

Reputation: 413

NSArray recursive search with index

I try to find item in array by index

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

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

Answers (1)

rmaddy
rmaddy

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

Related Questions