As the title states, I have found relatively old ones online about NSPropertyListSerialization, but I can’t find this class library to download on github...
溫故而知新,可以為師矣。 博客:www.ouyangke.com
The API is written very clearly. Use propertyListWithData:options:format:error: instead.[NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:NULL];
The system library cannot solve the problem?
Test code:
NSString *dataStr = @"求助~ Unicode 轉(zhuǎn) NSString";
NSString *utf8Str = [NSString replaceUnicode:dataStr];
NSLog(@" utf8Str = %@",utf8Str);
NSString *unnicodeStr = [NSString utf8ToUnicode:utf8Str];
NSLog(@" unicode = %@",unnicodeStr);
Run results:
Write these two methods into the NSString category
//Unicode轉(zhuǎn)UTF-8
+ (NSString*) replaceUnicode:(NSString*)aUnicodeString
{
NSString *tempStr1 = [aUnicodeString stringByReplacingOccurrencesOfString:@"\u" withString:@"\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""];
NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return [returnStr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"];
}
// utf8轉(zhuǎn)unnicode
+(NSString *) utf8ToUnicode:(NSString *)string
{
NSUInteger length = [string length];
NSMutableString *str = [NSMutableString stringWithCapacity:0];
for (int i = 0;i < length; i++) {
unichar _char = [string characterAtIndex:i];
//判斷是否為英文和數(shù)字
if (_char <= '9' && _char >= '0') {
[str appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];
} else if(_char >= 'a' && _char <= 'z') {
[str appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];
} else if(_char >= 'A' && _char <= 'Z') {
[str appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];
} else {
[str appendFormat:@"\u%x",[string characterAtIndex:i]];
}
}
return str;
}