获取汉字单词的首字母Swift版本

2016-03-08  本文已影响662人  酷酷的哀殿
let chineseString = "中国"

if #available(iOS 9.0, *) {
  if let str1 = chineseString.stringByApplyingTransform(NSStringTransformToLatin, reverse: false) {
    if let str2 = str1.stringByApplyingTransform(NSStringTransformStripCombiningMarks, reverse: false) {
      let index = str2.startIndex.advancedBy(1, limit: str2.endIndex)
      let str3 = str1.substringToIndex(index)
      print(str3)
    }
  }
} else {
  let str1:CFMutableString = CFStringCreateMutableCopy(nil, 0, chineseString);
  CFStringTransform(str1, nil, kCFStringTransformToLatin, false)
  CFStringTransform(str1, nil, kCFStringTransformStripCombiningMarks, false)
  let str2 = CFStringCreateWithSubstring(nil, str1, CFRangeMake(0, 1))
  print(str2)
}
上一篇下一篇

猜你喜欢

热点阅读