gpt4 book ai didi

ios - 尝试在 Swift 中打印表情符号时打印额外的 unicode

转载 作者:行者123 更新时间:2023-11-30 11:39:16 28 4
gpt4 key购买 nike

当我尝试像这样打印表情符号时:

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com