iOS search (can be implemented with fuzzy search support Pinyin retrieval first letter, etc.)

Source: Internet
Author: User

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.