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

标签 ios swift debugging unicode

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

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/

相关文章:

ios - UIGraphicsImageRenderer 生成 PPI 不正确的图像

ios - UIWebView 内容偏移量

ios - Swift:创建具有不同自定义类型的数组

ios - iPhone 重启后部署到 iphone 的 Xcode Swift 应用程序丢失 CoreData

ios - NSInternalInconsistencyException在将核心数据添加到应用程序时

ios - 选择器 valueWithCGRect 没有已知的类方法

swift - Xcode 6.1 Swift 问题 - 'init()' 不可用 : superseded by import of -[NSObject init]

linux - 是否可以边执行边调试程序?

xcode - 从控制台应用程序查看信息和调试日志

c++ - 在 carbide 2.7 中调试 exe 和 dll 项目