base64EncodedStringWithOptions in Swift fails with compile error

Swift 3.x

let fileStream = fileData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))

For Swift 2.x use an array for options:

let dataStr = data.base64EncodedStringWithOptions([.Encoding64CharacterLineLength])
let dataStr = data.base64EncodedStringWithOptions([])

Unless explicitly given an external name, first argument of a method in Swift is not a named argument. Therefore you should be doing: data.base64EncodedStringWithOptions(x) without the options: part.

If you actually look at the argument type, NSDataBase64EncodingOptions, you'll notice that it is a struct conforming to RawOptionSet with static variables for option constants. Therefore to use them you should do: NSDataBase64EncodingOptions.Encoding64CharacterLineLength

The NSDataBase64EncodingOptions struct (or RawOptionSet in general) is also not convertible from integer literals (like 0). But it does conform to NilLiteralConvertible so if you don't want any options you can pass nil.

Putting it together:

let dataStr = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

or

let dataStr = data.base64EncodedStringWithOptions(nil)

Swift3.0

let dataStr = data.base64EncodedString(options: [])

For swift 3.0+ use this ,

var dataStr = data.base64EncodedString(options: .lineLength64Characters)

Tags:

Ios

Base64

Swift