0

我使用 NSPredicate 从 NSMutableArray 中提取数据:

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.

4

4 回答 4

1

What's being returned is an array containing 1 object (which denoted by the curly braces {} means a dictionary). To extract each of the three components, you can do:

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"];

Bear in mind that this solution is only suitable for the example you've provided. If the query ever returns more than 1 object in the array, you'll need to use enumeration/for loop to read the results.

于 2012-07-21T13:13:17.747 回答
0

要将字典中的所有值放入数组中,请执行以下操作:

[dictionary allValues];
于 2012-07-21T15:06:03.720 回答
0

我知道你想分别得到这三个对象,不是吗?如果我是对的:

NSLog(@"PUBID: %d \n PUBNAME: %@ \n CITY: %@", [[results objectAtIndex:0] intValue], [results objectAtIndex:1], [results objectAtIndex:2]);

此代码应打印
PUBID: 102 PUBNAME: some publisher CITY: Peshawar。

所以你的结果来自

[array_to_search filteredArrayUsingPredicate:predicate];

是另一个数组,您可以将它与 objectAtIndex 一起使用:

于 2012-07-21T13:13:58.960 回答
0

您使用 prodicate 从数组中取出的对象显然是一个 NSDictionary。使用以下代码:

NSString *city = [[results objectAtIndex:0] valueForKey:@"city"];

等等。

于 2012-07-21T15:21:09.987 回答