0

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\u2705 Laugh \uD83D\uDE02\n\u2705 Read novels \uD83D\uDCDA\n\u2705 Watch movies \uD83C\uDFAC\n\u2705 Go for bike rides \uD83D\uDEB5\uD83C\uDFFD\u200D\u2640\uFE0F\n\u2705 Go for long walks \uD83D\uDEB6\uD83C\uDFFD\u200D\u2640\uFE0F\n\u2705 Cook \uD83D\uDC69\uD83C\uDFFD\u200D\uD83C\uDF73\n\u2705 Travel \uD83C\uDDEA\uD83C\uDDFA\uD83C\uDDEE\uD83C\uDDF3\uD83C\uDDEC\uD83C\uDDE7\n\u2705 Eat \uD83C\uDF2E\uD83C\uDF5F\uD83C\uDF73\n\u2705 Play board games \u265F\n\u2705 Go to the theatre \uD83C\uDFAD\nMy favourite season is autumn \uD83C\uDF42, i love superhero movies \uD83E\uDDB8\u200D\u2642\uFE0F and Christmas is the most wonderful time of the year! \uD83C\uDF84"

Here is the original text image: Text with Emoji

  • Does this answer your question? [Swift Encode/decode emojis](https://stackoverflow.com/questions/44220794/swift-encode-decode-emojis) – koen Mar 30 '20 at 14:06
  • No, I have checked this post but didn't find a solution. – Tushar Amkar Mar 30 '20 at 14:11
  • So can you explain what you mean by "the encoded string which is not decoding" ? Do you see nothing at all, or do you see garbage? – koen Mar 30 '20 at 14:24
  • I am getting just the same string after decoding the text. – Tushar Amkar Mar 30 '20 at 14:34
  • Please add some more code in your question as a [example] how you are calling your decode function. – koen Mar 30 '20 at 14:37
  • @koen, I have edited the question. please check it. I can't do more than that because I don't have any further information to post. – Tushar Amkar Mar 30 '20 at 14:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210598/discussion-between-tushar-amkar-and-koen). – Tushar Amkar Mar 30 '20 at 15:01

1 Answers1

2

The string you are using is invalid ("I love too...\n\u2705 Laugh \uD83D\uDE02\n\u2705 Read novels \uD83D\uDCDA\n\u2705 Watch movies \uD83C\uDFAC\n\u2705")

It should be in valid String literal "\\uD83D\\uDCDA\\u2705"

You have a string non-BMP characters in form of JSON String. And your decodeEmoji cannot convert them into valid characters.

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

First need to verify these things that json is valid or not before using.

USAGE:

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