Search for a phonetic search of IOS

Source: Internet
Author: User

1. Need to use third-party library PINYIN4OBJC2. Use method 2.1 to set the output format

Hanyupinyinoutputformat *fmt = [[Hanyupinyinoutputformat alloc]init];

Fmt.casetype = Casetypeuppercase; Indicates output by capital letter

Fmt.tonetype = Tonetypewithouttone; It means no tone.

Fmt.vchartype = Vchartypewithuunicode; V to u indicates

2.2 Performing conversions

NSString *pinyin = [pinyinhelper tohanyupinyinstringwithnsstring:@ "Beijing" withhanyupinyinoutputformat:fmt withNSString: @"#"];

Parameter 1:@ "Beijing", Chinese string to be converted

Parameter 2:fmt, output format

Parameter 3:@ "#", delimiter, can be an empty string @ ""

Result of printing string pinyin: bei#jing

3. Achieve the first letter of Pinyin search

NSString *pinyin2 = [Pinyinhelper toHanyuPinyinStringWithNSString:city.name withhanyupinyinoutputformat:fmt withnsstring:@ "#";

Nsarray *words = [pinyin2 componentsseparatedbystring:@ "#"];

nsmutablestring *headletterstr = [nsmutablestring string];

[Words enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {

[Headletterstr appendstring:[(NSString *) obj substringtoindex:1]];

}];

4. General search criteria, cite examples of urban search

if (([city.name rangeofstring:condition].length! = 0) | |

([pinyin rangeofstring:condition.uppercasestring].length! = 0) | |

([headletterstr Rangeofstring:condition.uppercasestring].length! = 0)

)

{

Search success, need to do things

}

5. Actual effect

Search for a phonetic search of IOS

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.