方法列舉
A. 最終版 項目中直接采用了這種方法抓歼,原因:處理速度快。
- (NSString *)transformToPinyin {
NSMutableString *mutableString = [NSMutableString stringWithString:self];
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
return [mutableString stringByReplacingOccurrencesOfString:@"'" withString:@""];
}
B. PinYin4Objc 項目中未采用此方法拢锹,因為該庫已不進(jìn)行維護(hù)
C. 網(wǎng)友推薦的方法
- (NSString *)transformToPinyin {
NSMutableString *mutableString = [NSMutableString stringWithString:self];
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false);
return mutableString;
}
D. 正則表達(dá)式優(yōu)先過濾英文數(shù)字特殊字符等
- (NSString *)transformToPinyin {
NSMutableString *mutableString = [NSMutableString stringWithString:self];
BOOL isNeedTransform = ![self isAllEngNumAndSpecialSign];
if (isNeedTransform) {
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false);
}
return mutableString;
}
- (BOOL)isAllEngNumAndSpecialSign {
NSString *regularString = @"^[A-Za-z0-9\\p{Z}\\p{P}]+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regularString];
return [predicate evaluateWithObject:self];
}
E. 參考NSHipster
- (NSString *)transformToPinyin {
NSMutableString *mutableString = [NSMutableString stringWithString:self];
BOOL isNeedTransform = ![self isAllEngNumAndSpecialSign];
if (isNeedTransform) {
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripCombiningMarks, false);
}
return mutableString;
}
- (BOOL)isAllEngNumAndSpecialSign {
NSString *regularString = @"^[A-Za-z0-9\\p{Z}\\p{P}]+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regularString];
return [predicate evaluateWithObject:self];
}
測試demo:CompareTransformDemo
-[ViewController pinyin4ObjC]: 0.185478
-[ViewController 最終版]: 0.602104
-[ViewController 網(wǎng)友推薦]: 12.419527
-[ViewController 正則表達(dá)式優(yōu)先過濾英文數(shù)字特殊字符等]: 13.215513
-[ViewController 參考NSHipster]: 7.774923
時間成本:PinYin4Objc < 最終版 << 其他方法
后續(xù)優(yōu)化
多音字處理
目前暫無好的辦法谣妻,可以手動列舉進(jìn)行處理,類似:
+ (NSString *)transformMandarinToLatin:(NSString *)hanzi { /* 部分多音字處理 */
NSString *pinyin = @"";
if ([hanzi compare:@"長"] == NSOrderedSame) {
pinyin = @"chang";
} else if ([hanzi compare:@"沈"] == NSOrderedSame) {
pinyin = @"shen";
} else if ([hanzi compare:@"廈"] == NSOrderedSame) {
pinyin = @"xia";
} else if ([hanzi compare:@"地"] == NSOrderedSame) {
pinyin = @"di";
} else if ([hanzi compare:@"重"] == NSOrderedSame) {
pinyin = @"chong";
} else if ([hanzi compare:@"行"] == NSOrderedSame) {
pinyin = @"xing";
}
return pinyin;
}
操作緩存
如果需要頻繁調(diào)用漢字轉(zhuǎn)拼音的方法(例如項目需求是通訊錄+索引面褐,每次處理顧客數(shù)據(jù)時都需要用到漢字轉(zhuǎn)拼音的方法)
拌禾,可以考慮加上緩存,推薦使用 YYCache展哭。
思路:將處理過的項存入 cache 中湃窍,每次啟動時將數(shù)據(jù)從 diskCache 放入 memoryCache,同時不要忘記清除緩存時的處理匪傍。