2015-02-05 111 views
0

我的數組是模型對象的集合,其字段爲name,address,arrTags。 這裏arrTags是一個NSArray。使用NSPredicate過濾文本是字符串數組的過濾數據數組

我必須根據arrTags值過濾我的數組。

例如。我有像倫敦,巴黎,澳大利亞這樣的價值觀。

我想顯示的數組索引,具有myArray的倫敦,如果我鍵入字母L.

到目前爲止,我已經做了以下內容:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"arrTags contains[c] %@", searchText]; 
    arr_searchResults =[[NSMutableArray alloc] initWithArray:[myArray filteredArrayUsingPredicate:predicate]]; 
    [image_view reloadData]; 

這顯示只有當我鍵入倫敦陣列在我的搜索欄中。 但是我想顯示數組,如果我開始鍵入L。

在此先感謝。

+0

嘗試使用'beginswith',而不是'contains'。 – 2015-02-05 17:26:18

+0

@IanMacDonald沒有運氣! – user3804063 2015-02-05 17:32:04

+0

@IanMacDonald,錯誤彈出說不能適用於它不是子字符串。 (因爲它是一個標籤數組),所以你可以建議我如何訪問數組索引像索引[0]或我必須枚舉使用塊.... – user3804063 2015-02-05 17:46:43

回答

1

如果你想從一個陣列(arrTaggs)內進行搜索,您應該使用

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY arrTags CONTAINS[c] %@", searchText]; 
+0

第一個彈出此錯誤:'NSInvalidArgumentException',原因: '無法在對象上執行正則表達式匹配( 倫敦 )。 – user3804063 2015-02-05 18:17:48

+0

第二個說::'NSInvalidArgumentException',原因:'不能做一個字符串操作的東西不是一個字符串(lhs =( london )rhs = l)' – user3804063 2015-02-05 18:19:51

+0

Try @「ANY arrTags contains [c ]%@「並讓我知道結果。 – 2015-02-05 18:51:06