In a recent project, encountered the problem of getting pinyin initials, looked up the method on the Internet, did not find the right to use, so he studied a bit, wrote the following methods, welcome to exchange, I hope to help you.
pyfirst.swift// get pinyin initials, support to take a sentence in each word pinyin first// Created by Zhou Honey ([email protected]) on 2015/6/1 (Children's Day).// Copyright (c) 2015 www.miw.cn. All Rights Reserved.//import Foundationfunc Pyfirst (string:string, _ Allfirst:bool=false)->String{ var py= "#" if let S = string { if s = = "" { return py } var str = cfstringcreatemutablecopy (nil, 0, s) CFSt Ringtransform (str, nil, Kcfstringtransformtolatin, Boolean (0)) cfstringtransform (str, nil, Kcfstringtransformstripcombiningmarks, Boolean (0)) py = "" if Allfirst {for x in (str as String). Componentsseparatedbystring ("") { py + = Pyfirst (x) } } else { py = (str as nsstring). Substringtoindex (1). Uppercasestring } } return py}/*//Call example, return #var s:string? Pyfirst (s) s = "People's Republic of China @hi wor\r\nld." Call example, return Zhrmghgwpyfirst (s,true)//Call example, return Zpyfirst (s) */
This may be the best present on children's Day.
Ben Five learning iOS: Using Swift to get pinyin initials, support to take one sentence in each word pinyin first letter