Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不能转换,不过解决了 #20

Open
nijat2018 opened this issue Dec 20, 2017 · 0 comments
Open

不能转换,不过解决了 #20

nijat2018 opened this issue Dec 20, 2017 · 0 comments

Comments

@nijat2018
Copy link

nijat2018 commented Dec 20, 2017

试了好多办法,不能转换。
但最后解决了:
ChineseToPinyinResource.m 里面有个读取unicode_to_hanyu_pinyin.txt的代码:

NSDictionary *dataMap=(NSDictionary *)[self cachedObjectForKey:kCacheKeyForUnicode2Pinyin];
if (dataMap) {
self->_unicodeToHanyuPinyinTable=dataMap;
}
else{
NSString *resourceName =[[NSBundle mainBundle] pathForResource:@"unicode_to_hanyu_pinyin" ofType:@"txt"];
NSString *dictionaryText=[NSString stringWithContentsOfFile:resourceName encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [dictionaryText componentsSeparatedByString:@"\r\n"];
__block NSMutableDictionary *tempMap=[[NSMutableDictionary alloc] init];
@autoreleasepool {
[lines enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSArray *lineComponents=[obj componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[tempMap setObject:lineComponents[1] forKey:lineComponents[0]];
}];
}
self->_unicodeToHanyuPinyinTable=tempMap;
[self cacheObjec:self->_unicodeToHanyuPinyinTable forKey:kCacheKeyForUnicode2Pinyin];
}

不过注意dataMap的值,有时候它可以!=nil,但是里面的 :

[dataMap allKey].count == 0

也就是是一个empty dictionary,但!=nil, 这样它不能初始化unicodeToHanyuPinyinTable了,只需要在if里面加上一下:

if (dataMap && [dataMap allKeys].count != 0)

就可以解决了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant