我有一个NSArray,其中包含带有表情符号代码的NSStrings,格式如下:
0x1F463
我现在如何将它们转换为格式正确的NSString?
使用这种方法,我可以生成一个“Emoji”-NSString:
NSString *emoji = [NSString stringWithFormat:@"\U0001F463"];
但这只适用于持续的NSStrings。我怎样才能转换整个NSArray?
答案 0 :(得分:4)
不是我最好的作品,但似乎有效:
for (NSString *string in array)
{
@autoreleasepool {
NSScanner *scanner = [NSScanner scannerWithString:string];
unsigned int val = 0;
(void) [scanner scanHexInt:&val];
NSString *newString = [[NSString alloc] initWithBytes:&val length:sizeof(val) encoding:NSUTF32LittleEndianStringEncoding];
NSLog(@"%@", newString);
[newString release]; // don't use if you're using ARC
}
}
使用四个样本值的数组,我得到四对赤脚。
答案 1 :(得分:2)
你可以这样做:
NSString *str = @"0001F463";
// Convert the string representation to an integer
NSScanner *hexScan = [NSScanner scannerWithString:str];
unsigned int hexNum;
[hexScan scanHexInt:&hexNum];
// Make a 32-bit character from the int
UTF32Char inputChar = hexNum;
// Make a string from the character
NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
// Print the result
NSLog(@"%@", res);