方法一:
+ (NSString *) chineseCharactersToPinyin:(NSString*)sourceString {
NSMutableString *source = [sourceString mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
return source;
}
方法二:
+ (NSString *) chineseCharactersToPinyin:(NSString*)sourceString {
NSMutableString *mutableString = [NSMutableString stringWithString:sourceString];
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
return mutableString;
}
方法二的运行速率比方法一快,故建议使用方法二。