1

I'm currently writing an App in Swift2 that communicates with a server (also written in Swift). The problem is, is that the framework i use for the Tcp connection does only support to send Strings but I want to send a NSData to the client.

converting the NSData to String is very easy:

String(theData)     // returns <62706c......0111 00000412>

but I don't know how to convert the String with the Data, that comes out of this function, back to NSData

Can somebody help me please?

Thanks in advance, Michael

Michael_mhr
  • 744
  • 1
  • 9
  • 27

1 Answers1

2

You need to encode the data using base64 encoding.

for example NSData -> String

let data = NSData()
let encodedBase64String = data.base64EncodedStringWithOptions([])

and decode it back String -> NSData

let decodedData = NSData(base64EncodedString: encodedBase64String, options: [])

You might pass appropriate encoding and decoding options if needed

vadian
  • 274,689
  • 30
  • 353
  • 361