0

I need convert string to utf8 , when I search I got this

let str = String(UTF8String: strToDecode.cStringUsingEncoding(NSUTF8StringEncoding)) 

some people said this add as String extension, where I should add this? and after that how I can get string with utf8 encode

joda
  • 711
  • 2
  • 8
  • 16

1 Answers1

2

This is how it should be done if you want to use extension.

let str = "1234567890"
    extension String {
    var utf8Array: [UInt8] {
        return Array(utf8)
    }
}

str.utf8Array

But I would recommend doing it simply like this

let result = [UInt8](str.utf8) 
Nitesh
  • 1,564
  • 2
  • 26
  • 53
  • this is work when I print it, but I need use that with api by alamofire library, when I send arabic or speces I have error – joda Aug 23 '16 at 19:19
  • here my question http://stackoverflow.com/questions/39095613/send-arabic-words-and-spaces-to-api-not-work-with-alamofire – joda Aug 23 '16 at 19:19