swift - 重音字母和表情符号

标签 swift swift3 emoji

我需要编码/解码表情符号,我正在使用这个代码

var encodeEmoji: String{
        if let encodeStr = NSString(cString: self.cString(using: .nonLossyASCII)!, encoding: String.Encoding.utf8.rawValue){
            return encodeStr as String
        }
        return self
    }

对于表情符号,一切都很好,但是当我编码重音字母(æ, ø, å)时,我有这样的东西 \346,\370,\345 谁能帮助避免转换重音字母,或者说我做错了什么。提前致谢!

最佳答案

一种方法是分别对每个字符进行编码,检测表情符号:

let testString = "abcdřšá😃😃😃"

let encoded = testString.unicodeScalars
    .map {
        guard $0.isEmoji else { return String($0) }
        let data = String($0).data(using: .nonLossyASCII, allowLossyConversion: true)!
        return String(data: data, encoding: .ascii)!
    }.joined()

print(encoded) // abcdřšá\ud83d\ude03\ud83d\ude03\ud83d\ude03

使用来自 this answerisEmoji 方法.

关于swift - 重音字母和表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48808859/

相关文章:

ios - 表情符号图标中断 REST 调用

ios - 使用 ios google sign in api - 这两个值是什么?

swift - <未知> :0: error: type 'Key' constrained to non-protocol type 'String'

swift - 如何在使用 Alamofire 的 POST 网络调用中仅发送一个字符串值作为参数?

ios - JSON解析后结果为nil

ios - 在不损失质量的情况下调整 SKSpriteNode 的大小

java - 在 JEditorPane 中设置内联文本和图像

css - Emojione 区域定义的 textarea 没有任何覆盖的 CSS 修复

ios - 如何自动滚动 UITableView 单元格

ios - 如何获取当前正在输入的单词?