2014-02-12 32 views
0

我正在使用解析,並試圖使用下面的代碼來嘗試檢查哪些用戶在數組@ @「Id1」中有某個字符串。但是我沒有收到任何對象。 whereKey:containsString僅用於搜索分析字符串值嗎?如果是這樣,我怎麼能搜索解析數組,看看他們是否包含一個字符串?提前致謝。在數組中解析查找字符串

NSString *searchId = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchId"]; 

PFQuery *query = [PFUser query]; //1 

[query whereKey:@"Id1" containsString:searchId]; 

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {//4 
    if (!error) { 
     NSLog(@"1"); 
     self.ObjectsArray = nil; 
     self.ObjectsArray = [[NSArray alloc] initWithArray:objects]; 
    } else { 
     [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Please make sure you have a proper device connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
    } 
}]; 

回答

2

如果您正在尋找完整的字符串,它會更容易。尋找一個單一值在陣列中,只使用whereKey:equalTo:如下所述:

https://parse.com/docs/ios_guide#queries-arrays/iOS

[query whereKey:@"Id1" equalTo:searchId]; 

這返回行,其中所述陣列中的至少一個條目是等於搜索準則(手柄號碼,字符串,布爾等)。

如果您真的想要查找數組中至少有一個項目包含一個字符串的記錄,那麼您將不得不變得棘手。

一個選項是有一個稱爲(例如)「Id1_search」的不可見字段。創建更新前的雲代碼處理程序,將所有數組值連接到逗號列表中,並轉換爲小寫字母。然後,您可以在該新字段上使用whereKey:containsString:

+0

非常感謝! –