var dogString = "\u{1F436}"for codeUnit in dogString.utf8{ print("\(codeUnit) ")}
//得到 240 159 144 182
如何通过这四个数字得到 "\u{1F436}"所代表的Dog Face字符
方法是先用这几个codeUnit做成NSData,然后再转成NSString,就能得到对应的字符串了。
import Foundation var bytes:[Byte]=[240,159,144,182] let data = NSData(bytes: bytes,length: bytes.count) let dogString:String = NSString(data: data,encoding: NSUTF8StringEncoding)
顺便一提,1F436这个16进制数,也能直接通过UnicodeScalar转换成字符串,就像下面这样
var dogInt:UInt32=0x1F436 let dog:String=UnicodeScalar(dogInt).escape(asASCII: false)
DOG FACE