使用特定文本过滤字典中的NSDictionary

时间:2013-07-11 08:15:49

标签: iphone objective-c nsarray nsdictionary nspredicate

我正在获取一个字典数组的Web服务repsonse。每个字典都有对象,其值本身就是另一个字典。我需要在这个响应中实现搜索,就像我输入“技术”并去搜索一样,我应该从数组中获得那些“技术中任何地方的技术”的字典,有解决方案

     {
         key1 =         {
    0 =             {
    "id" = 608;
    "b" = "Apple-Iphone";
     };
    1 =             {
    "id" = 609;
    "b" = "Iphone";
    };
    2 =             {
    "id" = 610;
    "b" = "Show Text";
    };

    };
    key2 = "Technology resources";
    "key3" =         {
    0 =             {
    "id" = 1608;
    "b" = "I love reading";
    };
    1 =             {
    "id" = 1609;
    "b" = "I prefer iphone to others";
    };
    2 =             {
                "id" = 1610;
                "b" = "Mobile technology is great.I am happy to a be developer";
            };

        };

        "key4" = "Mobile technology is the fun";
}

1 个答案:

答案 0 :(得分:1)

这是第一种方法

    NSMutableDictionary *dict;
    NSArray *allKeys = [dict allKeys];
    for (NSString  *key in allKeys)
    {
        NSDictionary *innerDict = [dict objectForKey:key];

        NSArray *allInnerKeys = [innerDict allKeys];
        for (NSString  *innerKey in allInnerKeys)
        {
            NSDictionary *mostInnerDict = [dict objectForKey:innerKey];
            NSString *b = [mostInnerDict objectForKey:b];
            NSString *search = @"Technology";
            NSString *sub = [b substringFromIndex:NSMaxRange([b rangeOfString:search])];
            if(sub)
            {
                // ADD mostInnerDict Object to your Results Array
            }

        }
    }

或者您可以尝试更简单的方法

在NSDATA中获取响应

将NSDATA转换为NSString

并在NSSTRING中搜索子字符串