first, the event response to enter a keyword in the search box
-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) searchtext{NSLog (@ "The keyword entered is---%@---%lu",
SearchText, (unsigned long) searchtext.length);
An array of search results needs to be emptied beforehand [self.resultarray removeallobjects]; Add more than a thread, otherwise a large amount of time, there are obvious cotton phenomenon//the best place in the database to search again, efficiency will be faster some dispatch_queue_t globalqueue = dispatch_get_global_queue (0,
0); Dispatch_async (Globalqueue, ^{if (Searchtext!=nil && searchtext.length>0) {//traverse all content that needs to be searched, Where Self.dataarray is the array for storing the total data for (Datamodel *model in Self.dataarray) {NSString *TEMPSTR = model
. Content;
-----------> Turn all search results into pinyin nsstring *pinyin = [self transformtopinyin:tempstr];
NSLog (@ "pinyin--%@", pinyin); if ([Pinyin rangeofstring:searchtext options:nscaseinsensitivesearch].length >0) {//Store search results SELF.R
Esultarray array [Self.resultarray Addobject:model];
} }}else{self.resultarray = [Nsmutablearray ArrayWithArray:self.dataArray];
//back to main thread Dispatch_async (Dispatch_get_main_queue (), ^{[_tableview reloaddata];
});
}); }
Second, get Chinese characters into phonetic strings Address Book fuzzy search support Pinyin retrieval first-letter spelling Chinese search
-(NSString *) Transformtopinyin: (NSString *) astring {//converted to variable string nsmutablestring *str = [nsmutablestring stringwit
Hstring:astring];
Cfstringtransform ((cfmutablestringref) str,null, kcfstringtransformmandarinlatin,no);
Convert to phonetic cfstringtransform with no tones ((cfmutablestringref) str,null, kcfstringtransformstripdiacritics,no);
Nsarray *pinyinarray = [str componentsseparatedbystring:@ "];
nsmutablestring *allstring = [nsmutablestring new];
int count = 0;
for (int i = 0; i < Pinyinarray.count. i++) {for (int i = 0; i < pinyinarray.count;i++) {
if (i = = count) {[Allstring appendstring:@ "#"];
Distinguish the first few letters [allstring appendformat:@ "%@", Pinyinarray[i]];
} [allstring appendstring:@ ","];
Count + +;
} nsmutablestring *initialstr = [nsmutablestring new];
Phonetic initials for (NSString *s in Pinyinarray) {if (s.length > 0) {[Initialstr appendstring: [s substringtoindex:1]];
} [allstring appendformat:@ "#%@", initialstr];
[Allstring appendformat:@ ", #%@", astring];
return allstring; }