搜索未知NSMutableArray深度的值 [英] Searching for a value in an unknown NSMutableArray depth

查看:57
本文介绍了搜索未知NSMutableArray深度的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我问了一个错误的问题,所以我已经编辑了原始问题.

Ok, i kind of asked the wrong question so I've edited the original question.

我正在将数组以及NSDictionaries存储在数组中.这是一种实用程序,没有设置结构,用户可以根据需要输入嵌套信息.

I'm storing Arrays within Arrays, as well as NSDictionaries. It's a utility kind of application and there is no set structure, the user can enter nested information as much as they require.

理想情况下,我需要一个方法来滚动给定设置参数(某种类型的类,也许是字典键)的数组的所有内容.这是一个例子.

Ideally I need a method to scroll through the entire contents of my array given a set parameter (a type of class, maybe a dictionary key). Here's an example..

NSMutableArray *array = [[NSMutableArray alloc]init];

NSMutableDictionary *enteredItem = [[NSMutableDictionary alloc]init];

[enteredItem setObject:@"i'm a title"       forKey:@"title"];
[enteredItem setObject:@"i'm an id"         forKey:@"id"];
[enteredItem setObject:@"i'm a description" forKey:@"description"];
[enteredItem setObject:@"i'm a timestamp"   forKey:@"timestamp"];
[enteredItem setObject:array                forKey:@"items"];


[array addObject:enteredItem];
[array addObject:anotherDictionary];
[array addObject:moreDictionaries];

因此,在上面的示例中,我需要找到包含@我是ID"的字典(并将其返回).

希望我的问题很清楚.感谢您提供的任何帮助.

Hopefully my question is clear. Thanks for any help you can offer.

推荐答案

递归方法是正确的,但是如果您不知道递归,我不确定代码示例是否很有用.这是一个可行的解决方案:

Recursive approach is correct, but I'm not sure the code samples were very helpful if you don't already know recursion. Here's a working solution:

添加以下方法:

- (id)findObjectWithKey:(id)key inArray:(NSArray *)array
{
    for (id object in array)
    {
        if ([object isKindOfClass:[NSArray class]])
        {
            return [self findObjectWithKey:key inArray:object];
        }
        else if ([object isKindOfClass:[NSDictionary class]])
        {
            return [self findObjectWithKey:key inDictionary:object];
        }
    }
    return nil;
}

- (id)findObjectWithKey:(id)key inDictionary:(NSDictionary *)dict
{
    for (id subKey in dict)
    {
        id object = [dict objectForKey:subKey];
        if ([subKey isEqual:key])
        {
            return object;
        }
        else if ([object isKindOfClass:[NSArray class]])
        {
            return [self findObjectWithKey:key inArray:object];
        }
        else if ([object isKindOfClass:[NSDictionary class]])
        {
            return [self findObjectWithKey:key inDictionary:object];
        }
    }
    return nil;
}

然后找到您的对象,只需说:

Then to find your object, just say:

id object = [self findObjectForKey:@"title" inArray:array];

要修改查找特定对象并返回字典键的方法,请执行以下操作:

To modify the methods to find a specific object and return the dictionary key, do this instead:

- (id)findKeyWithObject:(id)key inArray:(NSArray *)array
{
    for (id object in array)
    {
        if ([object isKindOfClass:[NSArray class]])
        {
            return [self findKeyWithObject:key inArray:object];
        }
        else if ([object isKindOfClass:[NSDictionary class]])
        {
            return [self findKeyWithObject:key inDictionary:object];
        }
    }
    return nil;
}

- (id)findKeyWithObject:(id)object inDictionary:(NSDictionary *)dict
{
    for (id key in dict)
    {
        id subObject = [dict objectForKey:key];
        if ([subObject isEqual:object])
        {
            return key;
        }
        else if ([subObject isKindOfClass:[NSArray class]])
        {
            return [self findKeyWithObject:object inArray:object];
        }
        else if ([subObject isKindOfClass:[NSDictionary class]])
        {
            return [self findKeyWithObject:object inDictionary:object];
        }
    }
    return nil;
}

然后找到您的钥匙,只需说:

Then to find your key, just say:

id key = [self findKeyWithObject:object inArray:array];

这篇关于搜索未知NSMutableArray深度的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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