NSArray的提取物项目
我使用NSPredicate提取一个NSMutableArray的数据:NSArray的提取物项目
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", value];
NSArray *results = [array_to_search filteredArrayUsingPredicate:predicate];
当我使用:
NSLog(@"%@", results);
我得到:
({pub_id = 102 "pub_name" = "some publisher" city = "Peshawar"});
我想提取的值全部3项pub_id
,pub_name
,city
。
返回的是一个包含1个对象的数组(其中包含大括号{}
表示字典)。为了提取这三个组件,你可以这样做:
NSString *pub_id = [[results objectAtIndex:0] valueForKey:@"pub_id"];
NSString *pub_name = [[results objectAtIndex:0] valueForKey:@"pub_name"];
NSString *city = [[results objectAtIndex:0] valueForKey:@"city"];
记住,这个解决方案仅适用于您所提供的例子。如果查询返回数组中的多个对象,则需要使用enumeration/for循环来读取结果。
点符号显示一个错误,而不是要求 - >,无论如何,因为城市,pub_id,pub_name显示错误 – 2012-07-21 14:30:59
什么意思,他们显示一个错误?编辑您的原始问题以包含您在array_to_search中存储的内容,并显示对象的结构。这将有助于我们进一步理解您的问题。 – TeaPow 2012-07-21 15:18:30
错误消息,我得到了“没有成员在'struct_objc_object'中的'城市'” – 2012-07-22 06:46:36
我明白你想要分别得到这三个对象,不是吗? 如果我是正确的:
NSLog(@"PUBID: %d \n PUBNAME: %@ \n CITY: %@", [[results objectAtIndex:0] intValue], [results objectAtIndex:1], [results objectAtIndex:2]);
此代码应打印
的pubId:102 PUBNAME:一些出版商 城市:白沙瓦。
所以从
你的结果[array_to_search filteredArrayUsingPredicate:谓语]。
是另一个数组,你可以使用与objectAtIndex:
我认为只有1个对象返回,1&2会给出错误 – 2012-07-21 14:31:58
我尝试过NSString * s = [NSString stringWithFormat:@“%@”,[results valueForKey:@“pub_name”]]并返回(\ n)发布商\ n) – 2012-07-21 14:36:38
你试过了吗?我不能尝试,但我几乎完全肯定这应该工作。 – 2012-07-21 14:36:49
为了让所有的值从字典到一个数组做:
[dictionary allValues];
获取另一个数组将使我回到原点NSString * s = [NSString stringWithFormat:@“%@”,[results valueForKey:@“pub_name”]]并返回(\ n某个发布者\ n) – 2012-07-22 06:50:39
你使用走出阵列的对象prodicate显然是一个NSDictionary。使用以下代码:
NSString *city = [[results objectAtIndex:0] valueForKey:@"city"];
et cetera。
, 谢谢 – 2012-07-22 08:35:57
xcode只是一个IDE,与这个问题无关。 – vikingosegundo 2012-07-21 13:10:12