我想从Nsarray包含NsDictionaries获取特定数据

时间:2014-11-19 07:04:50

标签: ios iphone ios7 nsdictionary

你好朋友我是ios.in我的应用程序中的新手我从网络服务获得Json响应。响应是Nsarray包含NsDictionaries.in的类型,dict有categeryName =%@。我想填充表视图cantains只有与该Categoryname匹配的产品名称String.i我从所有dict获取所有名称

     cell.textLabel.text = [[songArray objectAtIndex:indexPath.row] objectForKey:@"Name"];

这是我的数组

value =     (
            {
        AreaBaseKey = "728b0e3e-295a-459d-9dca-56084afdeba8";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;

    },
            {
        AreaBaseKey = "c666bd31-ac3c-4028-a983-51fb9063e0dc";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;

    },
            {
        AreaBaseKey = "694c4b74-0a66-4f8d-b14c-8c53dfe8696f";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;
               },
            {
        AreaBaseKey = "cd60fb89-8aae-4f02-925b-8e91ccd648b3";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;
                },
            {
        AreaBaseKey = "54807449-9159-4701-9e72-d4cdf0e8163d";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;
                },
            {
        AreaBaseKey = "1ec28a54-c0d1-4f99-966d-02f83ece35d2";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;
                },
            {
        AreaBaseKey = "996cb68e-f950-e311-93f8-001a640a73ee";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;
                },
            {
        AreaBaseKey = "a6dac1ba-744b-e311-93f5-001a640a73ee";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;
              },
            {
        AreaBaseKey = "8c36f9e0-744b-e311-93f5-001a640a73ee";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;
               },
            {
        AreaBaseKey = "ec0c9f6b-754b-e311-93f5-001a640a73ee";
        CategoryKey = "9abcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Topuri;
        Code = 000000178242;
        IsActive = 1;
        Name = Chiloti;

    },
            {
        AreaBaseKey = "1a96ca82-a2f1-48d2-91c3-a14ae3d09f70";
        CategoryKey = "8cbcd7cb-6e4b-e311-93f5-001a640a73ee";
        CategoryName = Pachete;
        Code = 100;
        IsActive = 0;
        Name = 3pizza;
                },

我正在做这个

{   NSString *str = self.AppDelegate.indexname;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY CategoryName LIKE %@",str];
    NSArray *result = [songArray filteredArrayUsingPredicate:predicate];
    NSLog(@"%@",result);
}

还有

{
 NSString *str=self.AppDelegate.indexname;
 NSMutableArray *arr = [[NSMutableArray alloc]init];
    for(NSDictionary *dict in songArray)
 {
        if([[dict objectForKey:@"CategoryName"]isEqualToString:str])
    {
        [arr addObject:dict];
        NSLog(@"%@",arr);
    }
 }

}

但是没有获得输出我想要的是这行中的Errror NSArray * result = [songArray filteredArrayUsingPredicate:predicate];

2 个答案:

答案 0 :(得分:1)

您可以使用NSPredicate来过滤NSArrayNSMutableArray NSDictionaryNSMutableDictionary

NSString *str = <search string>;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY CategoryName LIKE %@", str];
NSArray *result = [value filteredArrayUsingPredicate:predicate]; 

了解更多NSPredicate结帐链接 - How To Create Predicates

答案 1 :(得分:0)

然后,您必须首先过滤“CategoryName”和“Name”匹配的词典的songArray,并将其存储在另一个数组中。然后在dataSource方法中使用该数组。

以下是如何做到这一点,(在解析JSON响应后添加以下代码)

NSMutableArray *arr = [NSMutableArray new];
for(NSDictionary *dict in songArray) {

if(dict@[CategoryName] == dict@[Name]) 
[arr addObject:dict];

}

现在在tableView数据源方法中使用数组“arr”(即numberOfRowsInSection:和cellForRowAtIndexPath:方法)。