简体   繁体   English

带有表情符号的文本未解码 - iOS Swift

[英]Text with emoji is not decoding - iOS Swift

I have used the following code to encode/decode the string which has emojis.我使用以下代码对包含表情符号的字符串进行编码/解码。

extension String {
    func encodeEmoji() -> String {
        let data = self.data(using: .nonLossyASCII, allowLossyConversion: true)!
        return String(data: data, encoding: .utf8)!
    }

    func decodeEmoji() -> String? {
        let data = self.data(using: .utf8)!
        return String(data: data, encoding: .nonLossyASCII)
    }
}

I have called this function like this below.我在下面这样调用了这个函数。 Converted the response in the 'User' model.转换了“用户”模型中的响应。

let user = User() // Loaded API's response in this model
let textWithEmoji = user.aboutMe.decodeEmoji() //Here, I am getting the string as the same as before decoding
lblAboutMe.text = textWithEmoji

Following is the encoded string which is not decoding:以下是未解码的编码字符串:

"I love too...\\n\✅ Laugh \?\?\\n\✅ Read novels \?\?\\n\✅ Watch movies \?\?\\n\✅ Go for bike rides \?\?\?\?\‍\♀\️\\n\✅ Go for long walks \?\?\?\?\‍\♀\️\\n\✅ Cook \?\?\?\?\‍\?\?\\n\✅ Travel \?\?\?\?\?\?\?\?\?\?\?\?\\n\✅ Eat \?\?\?\?\?\?\\n\✅ Play board games \♟\\n\✅ Go to the theatre \?\?\\nMy favourite season is autumn \?\?, i love superhero movies \?\?\‍\♂\️ and Christmas is the most wonderful time of the year! \?\?" “我也喜欢...\\n\✅ 笑\?\?\\n\✅ 看小说\?\?\\n\✅ 看电影\?\?\\n\✅ 去骑自行车\?\?\? \?\‍\♀\️\\n\✅ 长距离散步 \?\?\?\?\‍\♀\️\\n\✅ 做饭 \?\?\팍FuD\?uD\? n\✅ 旅行\?\?\?\?\?\?\?\?\?\?\?\?\\n\✅ 吃\?\?\?\?uDuDuDuD7棋盘游戏\♟\\n\✅去影院\?\?\\n我最喜欢的季节是秋天\?\?,我喜欢超级英雄电影\?\?\‍\♂\️,圣诞节是最美妙的时光年!\?\?"

Here is the original text image:这是原始文本图像: 带有表情符号的文本

The string you are using is invalid (" I love too...\\n\✅ Laugh \?\?\\n\✅ Read novels \?\?\\n\✅ Watch movies \?\?\\n\✅ ")您使用的字符串无效(“我也喜欢...\\n\✅ 笑\?\?\\n\✅ 阅读小说\?\?\\n\✅ 看电影\?\?\\n\✅ ”)

It should be in valid String literal "\\\?\\\?\\\✅"它应该是有效的字符串文字“\\\?\\\?\\\✅”

You have a string non-BMP characters in form of JSON String.您有一个 JSON 字符串形式的字符串非 BMP 字符。 And your decodeEmoji cannot convert them into valid characters.而且您的 decodeEmoji 无法将它们转换为有效字符。

So we need to forcefully convert such strings.所以我们需要对这样的字符串进行强制转换。

extension String {
    var jsonStringRedecoded: String? {
        let data = ("\""+self+"\"").data(using: .utf8)!
        let result = try! JSONSerialization.jsonObject(with: data, options: .allowFragments) as! String
        return result
    }
}

After that you need to decode emoji from above string using below function.之后,您需要使用以下函数从上面的字符串解码表情符号。

extension String {  
    var decodeEmoji: String? {
          let data = self.data(using: String.Encoding.utf8,allowLossyConversion: false);
          let decodedStr = NSString(data: data!, encoding: String.Encoding.nonLossyASCII.rawValue)
          if decodedStr != nil{
            return decodedStr as String?
        }
          return self
    }
}

Usually JSON decoder can decode these type of characters into emoji May be there is chances of invalid JSON通常 JSON 解码器可以将这些类型的字符解码为 emoji 可能存在无效 JSON 的机会

First need to verify these things that json is valid or not before using.在使用之前首先需要验证json这些东西是否有效。

USAGE:用法:

let jsonDecodedString = "Your string".jsonStringRedecoded
let decodedEmojiText = jsonDecodedString?.decodeEmoji
debugPrint("\(decodedEmojiText)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM