2017年2月7日 | Leave a comment 问题: ios 怎么把汉字转换成拼音? 解决: import Foundation extension String { func transformToPinYin() -> String { let mutableString = NSMutableString(string: self) //把汉字转为拼音 CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false) //去掉拼音的音标 //CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false) let string = String(mutableString) //去掉空格 //return string.stringByReplacingOccurrencesOfString(" ", withString: "") return string.replacingOccurrences(of: " ", with: "") } } 123456789101112131415161718 import Foundation extension String { func transformToPinYin() -> String { let mutableString = NSMutableString(string: self) //把汉字转为拼音 CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false) //去掉拼音的音标 //CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false) let string = String(mutableString) //去掉空格 //return string.stringByReplacingOccurrencesOfString(" ", withString: "") return string.replacingOccurrences(of: " ", with: "") }} print(“中国”.tranformToPinYin()) 输出 : zhōngguó 参考: http://blog.csdn.net/potato512/article/details/51108815