Search code examples
swiftbase64nsdata

Swift NSData from base64encoded string returns nil


I'm communicating with a server in Swift retrieving image data. The incoming data is encoded as a base64 string. I am able to correctly receive and display the encoded strings. When I go to use the NSData class to decode the string back to binary data and display...

println(NSData(base64EncodedString: imageString, options: NSDataBase64DecodingOptions(0)))

The output is

nil
nil
nil
nil
nil
nil

One for each of the images received.

I've also tried

println(NSData(base64EncodedString: imageString, options: nil))

and the same results. Is there anything I am missing along the way?? I would put the image strings up but they are massively long...


Solution

  • Try to use IgnoreUnknownCharacters option.

    Or try to use initWithBase64EncodedString from NSDataAdditions