Written by Sean Behan on Wed May 02nd 2018

Here is an extension to base 64 encode and decode strings in Swift (3)

extension String {
        func fromBase64() -> String? {
                guard let data = Data(base64Encoded: self) else {
                        return nil
                }
                return String(data: data, encoding: .utf8)
        }
        func toBase64() -> String {
                return Data(self.utf8).base64EncodedString()
        }
}

And use it like so...

let str = "Hello World"
str.toBase64().fromBase64()

Tagged with..
#swift #ios #base64 #encoding #strings

Just finishing up brewing up some fresh ground comments...