iOS开发之一个方法实现任意内容繁简互转(非本地化)

##摘要
本文将介绍我自己封装的一个任意内容繁简换转的实现思路。

##FGReverser
……………………………………………………..

##Introduction
A Category of NSString used for mutual converting between simplified Chinese and Traditional Chinese.

##Installtion
Manual:

Download This Project and drag the FGReverser folder into your peroject, do not forget to ensure “copy item if need” being selected.

Cocoapods:

1
pod 'FGReverser', '~> 1.0.0'

##Usage
Just import the header file:import "NSString+FGReverser.h"

Simplfied Chinese to Traditional Chinese, or Traditional Chinese to Simplfied Chinese, Use:

1
-(NSString *)reverseString;

##Example

1
2
3
NSString *sourceString=@"恭喜发财";
NSString *reslutString=[sourceString reverseString];
NSLog(@"%@",reslutString);

The result is 恭喜發財

##About Me

……………………………………………………..

@CGPoitZero

##实现思路
上面内容是该项目的简介,该拓展仅用了一个方法就实现了任意内容繁简体互转的功能。

1.收集简体和繁体编码的字符集
2.整理出来繁体和简体不一样的文字
3.分别将简体和繁体不一样的字中所有的简体字和繁体字加载到两个不同的数组中,并且确保这两个数组中的简体和繁体一一对应(相同的index)
4.搞一个可变字典,遍历简体字数组,以简体字为键,以繁体字为值,以key-value的形式放入字典中,并且以繁体字作为键,以简体字作为值,再存一遍,即

1
2
3
4
5
6
for(long i=0i<simplifiedArray.count; i++){
NSString *key=[simplifiedArray objectAtIndex:i];
NSString *value=[traditionalArray objectAtIndex:i];
[dict setValue:value forKey:key];
[dict setValue:key forKey:value];
}

5.将上一步的字典写入一个文件,最好是写入Mac上你知道的一个路径,作为资源文件,比如我存到了桌面上,叫reverse

1
[dict writeToFile:filePath atomically:YES];

##进入正题
上面的都是准备工作,代码中不需要用到,只是为了准备资源文件。

###思路
为了方便使用,我直接搞了个NSString的分类,当然也可以搞成一个单列类。

1
2
3
4
5
/**
* Mutual converting
*
*/
@interface NSString (FGReverser)

加载上一步的资源文件到一个字典中,为下面的转化做准备。

1
2
NSString *mapPath=[[NSBundle mainBundle] pathForResource:@"reverse" ofType:nil];
NSDictionary *map=[NSDictionary dictionaryWithContentsOfFile:mapPath];

搞一个繁简互转的接口,在这个接口中,去遍历给定的字符串,取出每个字符对应的反转字符

1
2
3
4
5
6
/**
* Mutual converting between Simplfied Chinese and traditional Chinese.
*
* return the converted string
*/
-(NSString *)reverseString;

##互转逻辑
很简单,把要转化的字符串,逐个取出来(unichar型的,用格式说明符号%C转成NSString)。

1
2
unichar c=[resultString characterAtIndex:i];
NSString *key=[NSString stringWithFormat:@"%C",c];

然后到上面加载好的map中检查是否存在反转值。若存在反转值,则用反转值字符替换旧值字符,最后将处理好的字符串返回,就实现了繁简互转。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Mutual converting between Simplfied Chinese and traditional Chinese.
*
* return the converted string
*/
-(NSString *)reverseString{

NSString *mapPath=[[NSBundle mainBundle] pathForResource:@"reverse" ofType:nil];
NSDictionary *map=[NSDictionary dictionaryWithContentsOfFile:mapPath];
NSMutableString *resultString=[NSMutableString stringWithString:[self copy]];

for (NSInteger i=0; i<resultString.length; i++){

unichar c=[resultString characterAtIndex:i];
NSString *key=[NSString stringWithFormat:@"%C",c];
NSString *value=[map objectForKey:key];
if(value){

[resultString deleteCharactersInRange:NSMakeRange(i, 1)];
[resultString insertString:value atIndex:i];
}
}
return resultString;
}

至此已实现全部功能。
最后附上我在Github上的代码地址:FGReverser,欢迎pull request和star~,希望多支持。
同时,你也可以从Cocoapods下载到:FGGReverser,使用也很简单,直接:

1
pod 'FGReverser', '~>1.0.0'

我的博客地址:CGPointZero,欢迎访问收藏,一起探讨。