2012-07-21 43 views
0

我使用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

+2

xcode只是一個IDE,與這個問題無關。 – vikingosegundo 2012-07-21 13:10:12

回答

1

返回的是一個包含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循環來讀取結果。

+0

點符號顯示一個錯誤,而不是要求 - >,無論如何,因爲城市,pub_id,pub_name顯示錯誤 – 2012-07-21 14:30:59

+0

什麼意思,他們顯示一個錯誤?編輯您的原始問題以包含您在array_to_search中存儲的內容,並顯示對象的結構。這將有助於我們進一步理解您的問題。 – TeaPow 2012-07-21 15:18:30

+0

錯誤消息,我得到了「沒有成員在'struct_objc_object'中的'城市'」 – 2012-07-22 06:46:36

0

我明白你想要分別得到這三個對象,不是嗎? 如果我是正確的:

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:

+0

我認爲只有1個對象返回,1&2會給出錯誤 – 2012-07-21 14:31:58

+0

我嘗試過NSString * s = [NSString stringWithFormat:@「%@」,[results valueForKey:@「pub_name」]]並返回(\ n)發佈商\ n) – 2012-07-21 14:36:38

+0

你試過了嗎?我不能嘗試,但我幾乎完全肯定這應該工作。 – 2012-07-21 14:36:49

0

爲了讓所有的值從字典到一個數組做:

[dictionary allValues]; 
+0

獲取另一個數組將使我回到原點NSString * s = [NSString stringWithFormat:@「%@」,[results valueForKey:@「pub_name」]]並返回(\ n某個發佈者\ n) – 2012-07-22 06:50:39

0

你使用走出陣列的對象prodicate顯然是一個NSDictionary。使用以下代碼:

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

et cetera。

+0

, 謝謝 – 2012-07-22 08:35:57