当我尝试像这样打印表情符号时:
print("Incomplete frame ⚠️")
有时它在调试器中打印为:
Incomplete frame ⚠⚠\357️
这个也一样:
print("Frame not ready 💤")
有时(但并非总是!)显示:
Frame not ready \360\237\222💤
或者,更糟糕的是,比如
Frame not ready \360\237\360\237\360\237\222💤
为什么会发生这种情况?我该如何预防?
最佳答案
您必须将 Unicode 字符串转换为属性字符串,请使用下面的字符串扩展
extension String
func UnicodeToAttributed() -> NSAttributedString? {
do {
let data = self.data(using: String.Encoding.utf8, allowLossyConversion: true)
if let d = data {
let str = try NSAttributedString(data: d,
options: [
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
documentAttributes: nil)
return str
}
} catch {
}
return nil
}
}
关于ios - 尝试在 Swift 中打印表情符号时打印额外的 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49424825/