2012-02-16 89 views
0

我有NSMutableArray如下如何从NSMutableArray中取数据取决于其内的内容?

我如何只提取那些有experience_id=1或更新这些数据的数据?

有没有其他方式没有循环?

(
      { 
      "exp_achievements" = "get certificate for best employee"; 
      "exp_achive_id" = 1; 
      "experience_id" = 1; 
     }, 
    { 
      "exp_achievements" = "get certificate for best employee test"; 
      "exp_achive_id" = 3; 
      "experience_id" = 1; 
     }, 
      { 
      "exp_achievements" = test2; 
      "exp_achive_id" = 5; 
      "experience_id" = 3; 
     } 
    ) 
+0

告诉我,你怎么能访问整个阵列在一次 – Hiren 2012-02-16 07:29:30

+0

我有一次所有的数据,然后想要过滤的数据,如为perticular exprience_id – Heena 2012-02-16 07:33:24

+0

@Roshni是这个一个json字符串? – rakeshNS 2012-02-16 07:36:00

回答

2

您可以使用NSPredicate来筛选集合:

NSMutableArray *a = [NSMutableArray arrayWithObjects: 
         [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:@"experience_id"], 
         [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:2] forKey:@"experience_id"], nil]; 
NSPredicate *f = [NSPredicate predicateWithFormat:@"experience_id == 1"]; 
[a filterUsingPredicate:f]; 

打印:

f:(
     { 
     "experience_id" = 1; 
    } 
) 

如果你的 'experience_id' 值是字符串,改变格式@"experience_id.intValue == 1"

+0

这将返回空数组 – Heena 2012-02-16 08:29:01

+0

如果在您的内部字典对象中的键'experience_id'是数字 - 那么它的工作原理,测试。如果它是字符串 - 则谓词格式应该是“experience_id LIKE [cd]'1'”。 – mit3z 2012-02-16 08:45:23

+0

它是number.as你可以在我的问题中看到在答复打印。虽然它对'@“exp_achievements =='test2'”'工作正常,但对于exprience_id – Heena 2012-02-16 08:56:58

2

试图用NSPredicate与关键experience_id = 1的值我不知道,但尝试它可以帮助ü

+0

我如何使用nspredicate和nsmutablearray? – Heena 2012-02-16 07:37:45

+0

正如peter所说,使用nspredicate来过滤使用预测NSPredicate * pre = [NSPredicate filteredArrayUsingPredicate:temp_details]; – 2012-02-16 07:53:49

1

你应该能够做到这一点使用filteredArrayUsingPredicate:(从NSArray),你必须创建一个NSPredicate,它可以过滤您所需的密钥。它返回一个包含匹配项的数组。在NSMutableArray中还有filterUsingPredicate,用于删除与谓词不匹配的项目。