Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode unicode characters in swift 3? [duplicate]

I am developing one chat application, for that everything is working fine except. This application is in both Android and iOS platform. We want to pass emoji in the chat. In android we use UTF encoding with StringEscapeUtils. it is working perfect in android. when we pass emoji enter image description here it is encoded and stored in DB like "\u263A".

Now in android this string is also decode and shown perfect in view but some how we can not decode the same string in iOS. We have simply try to decode using UTF string. But still it is not working.

I have already follow this link Print unicode character from variable (swift)

Thanks in advance.

like image 501
Crazy Developer Avatar asked Dec 08 '16 06:12

Crazy Developer


2 Answers

The easiest way is to use CFStringTransform like below.

let wI = NSMutableString( string: "\\u263a" )
CFStringTransform( wI, nil, "Any-Hex/Java" as NSString, true )
someUILabel.text = wI as String
like image 136
Satachito Avatar answered Oct 12 '22 23:10

Satachito


You should try this:

For Example:

let charString = "263A"
    if let charCode = UInt32(charString, radix: 16),let unicode = UnicodeScalar(charCode)
    {
        let str = String(unicode)
        print(str)
    }
    else
    {
        print("invalid input")
    }

If you want to Print on Label/TextField then:

let charString = "263A"
if let charCode = UInt32(charString, radix: 16),let unicode = UnicodeScalar(charCode)
{
    let str = String(unicode)
    CharLabel.text = str             //Print on Label
    CharTextField.text = str         //Print on TextField
}
else
{
    print("invalid input")
}
like image 27
Pragnesh Vitthani Avatar answered Oct 12 '22 23:10

Pragnesh Vitthani