I have the following base64 encoded string and I need to base64 decode it using Swift:
KimHser2RvFf9RPjajWO4K/odT51hTlISwMKNIfPUC+gXYZKNjGDCvHEom++6liXNq6PkStnpzMKBsTk+tIpJA==
There is no line break there, StackOverflow is just wrapping the string. I have tried numerous methods to base64 decode this. I do receive a result as NSData, however, when I try to convert the NSData to an NSString object, the result is always nil. If I change the encoding to ASCII or UTF16 I receive a result, so I assume Swift is not recognizing the NSData in UTF8 format. However, if I base 64 decode the same string in C#, UTF8 works, and the Base64 decoding works. So I'm positive I'm missing a step due to my lack of experience with Swift.
Here is my latest attempt:
import Foundation
extension String {
// MARK: Base64 encode
func base64Encode() -> String {
let data : NSData = self.dataUsingEncoding(NSUTF8StringEncoding)!
return data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
// MARK: Base64 decode
func base64Decode() -> String {
let decodedData = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
return decodedString as! String
}
}
I'm positive it's something easy. However, this is my first application in Swift and I'm still learning.
Any help is much appreciated.
Thanks!
decodedDatais<2a2987b1 ... fad22924>which is definitely not a valid UTF-8 sequence. What result do you expect?