如何获得树级的NSMutableArray

时间:2013-04-28 06:14:56

标签: ios objective-c nsarray

我有一个树NSMutableArray作为treeChannelModel.data,我想生成treeChannelModel.data的树级NSMutableArray。例如,我想获得树级别:

NSMutableArray= {0,1,2,2,2,2,2,2,0,1,2,2,2,2,2,3,3,2,2,2,2,3,2,1,2,3,3,2,1,2,3,3,3,2,3,3,2,3,3,3,2,3,2,3,2,3,2,3,2,3,2,3};

NSMutableArray *depths1 = [NSMutableArray array];
for(int i=0;i<treeChannelModel.data.count;i++){
    depths1=[self ChildIntRecursiveWithChannels:channels depths:depths1 number:0];
}

- (NSMutableArray *)ChildIntRecursiveWithChannels:(Channels *)channels  depths:(NSMutableArray *) depths number:(int)number {
    number+=1;
    [ depths addObject:[NSNumber numberWithInt:number]];

    if(channels.channels.count>0){
        for(int i=0;i<channels.channels.count;i++){
            [self ChildIntRecursiveWithChannels:channels depths:depths number:number];
        }
    }
    return  depths;
}

但是我得到了一个不正确的数组。你可以给出一些建议

1 个答案:

答案 0 :(得分:0)

- (NSMutableArray *)ChildIntRecursiveWithChannels:(Channels *)channels
                                           depths:(NSMutableArray *)depths
                                           number:(int)number {

    [depths addObject:[NSNumber numberWithInt:number]];
    number += 1;

    NSLog(@"channels.channels.name:%@", channels.name);

    for(int i = 0; i < channels.channels.count; i++){
        [self ChildIntRecursiveWithChannels:channels.channels[i] depths:depths number:number];
    }
    return  depths;
}