Value of type 'Data?' has no member 'base64EncodedStringWithOptions'

10.5k Views Asked by At

Here I am encoding my string but it gives an error stated above. What I had done is:

let plainData = (password)?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
let base64String = plainData.base64EncodedStringWithOptions(NSData.Base64EncodingOptions.init(rawValue: 0))

It gives me an error on the second line of code. If anyone can help!

2

There are 2 best solutions below

1
On

Actually with update of swift version it gives an error. We can do something like:

let plainData = (password)?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
let base64String = plainData?.base64EncodedData(options: NSData.Base64EncodingOptions.init(rawValue: 0))

It solved my problem.

0
On

You need to use base64EncodedString() function of NSData.

let base64String = plainData?.base64EncodedString()

This works with Swift 3.0