递归遍历未知结构的NSDictionary [英] Recursively traverse NSDictionary of unknown structure

查看:60
本文介绍了递归遍历未知结构的NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人对未知结构的 NSDictionary 进行过递归有序遍历?我想使用任何 NSDictionary 并按层次顺序处理每个级别.

Has anyone done a recursive ordered traversal of an NSDictionary of unknown structure? I'd like to take any NSDictionary and process each level in hierarchical order.

1) 此数据来自经过验证的 JSON.可以肯定地说,从 SBJSON(JSON 框架)等框架创建的 NSDictionary 只会导致嵌套字典、数组和任意叶子的组合?

1) This data is coming from validated JSON. Is it safe to say that the NSDictionary created from a framework such as SBJSON (JSON Framework) would only result in a combination of nested dictionaries, arrays, and arbitrary leafs?

2) 如何使用适用于数组和字典的快速枚举来完成通用遍历?使用下面的代码,一旦我到达数组中的字典,它就会停止遍历.但是,如果我在数组条件中继续递归(检查数组中的字典),它会在 id value = [dict valueForKey:key]; 的下一次迭代中使用 -[__NSCFDictionary length]:无法识别的选择器发送到实例 SIGABRT.我不知道为什么这会是一个问题,因为我已经通过顶级字典(找到了子级字典数组)超过了该行.

2) How can a generic traversal be done using fast enumeration that works for both arrays and dictionaries? With the code below, once I get to a dictionary within an array, it stops traversing. However, if I continue the recursion in the array condition (to check for dictionaries within arrays), it barfs on the next iteration of id value = [dict valueForKey:key]; with a -[__NSCFDictionary length]: unrecognized selector sent to instance SIGABRT. I don't know why this would be a problem, because I already got past that line with a top-level dictionary (where the array of sub-level dictionaries were found).

-(void)processParsedObject:(id)dict counter:(int)i parent:(NSString *)parent
{
    for (id key in dict) {
        id value = [dict valueForKey:key];
        NSLog(@"%i : %@ : %@ -> %@", i, [value class], parent, key);

        if ([value isKindOfClass:[NSDictionary class]])
        {
            i++;
            NSDictionary* newDict = (NSDictionary*)value;
            [self processParsedObject:newDict counter:i parent:(NSString*)key];
            i--;
        }
        else if ([value isKindOfClass:[NSArray class]])
        {
            for (id obj in value) {
                NSLog(@"Obj Type: %@", [obj class]);
            }
        }
    }
}

非常感谢

推荐答案

我做过类似的事情,我将遍历来自 Web 服务的 JSON 结构化对象并将每个元素转换为可变版本.

I've done something similar where I would traverse a JSON structured object coming from a web service and convert each element into a mutable version.

- (void)processParsedObject:(id)object
{
    [self processParsedObject:object depth:0 parent:nil];
}

- (void)processParsedObject:(id)object depth:(int)depth parent:(id)parent
{      
    if ([object isKindOfClass:[NSDictionary class]])
    {
        for (NSString* key in [object allKeys])
        {
            id child = [object objectForKey:key];
            [self processParsedObject:child depth:(depth + 1) parent:object];
        }
    }
    else if ([object isKindOfClass:[NSArray class]])
    {
        for (id child in object)
        {
            [self processParsedObject:child depth:(depth + 1) parent:object];
        }   
    }
    else
    {
        // This object is not a container you might be interested in it's value
        NSLog(@"Node: %@  depth: %d", [object description], depth);
    }
}

这篇关于递归遍历未知结构的NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆