2014-02-18 36 views
0

我的項目中有searchBarsearchController。我希望能夠搜索特定的字母序列,如果可能的話可能會跳過一個或兩個字符。例如,如果我搜索「iPhone」或「i-phone」或「i phone」或「piphone」或「phone」,我希望能夠在列表中找到搜索詞「iphone」。任何幫助,將不勝感激。使用UISearchBar搜索字符序列

哪裏SEARCHTERM是的UISearchBar

NSCharacterSet *allowedChars = [[NSCharacterSet characterSetWithCharactersInString:@" ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890"] invertedSet]; 
NSString *resultString = [[searchTerm componentsSeparatedByCharactersInSet:allowedChars] componentsJoinedByString:@" "]; 

全大寫的字符串以記住,@「‘在allowsChars是必需的,因爲實際搜索可@’iPhone 32GB」等...

+0

我不知道該怎麼繼續。我所做的唯一嘗試就是將最終字符串轉換爲所有大寫字母並刪除任何特殊字符。我將使用代碼 – snksnk

+0

編輯帖子您嘗試使用'NSPredicate'?你的數據是數組中的字符串還是數組中的字典? – Wain

+0

數據將從雲數據庫 – snksnk

回答

1
NSArray *firstNames = [[NSArray alloc] initWithObjects: @"i-phone", @"i phone", @"phone", @"Quentin",@"apple phone",@"phoapple" ]; 
    NSString * searchStr = @"phone"; 
    NSPredicate *thirtiesPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@",searchStr]; 
    NSLog(@"Bobs: %@", [firstNames filteredArrayUsingPredicate:thirtiesPredicate]); 

它將記錄爲

2014-02-18 06:09:52.911 demo[5261] Bobs: ("i-phone", "i phone", phone, "apple phone")