base64EncodedStringWithOptions in Swift fails with compile error

let dataStr = data.base64EncodedStringWithOptions(options: Encoding64CharacterLineLength)

Doesn't compile with "Use of unresolved identifier 'Encoding64CharacterLineLength'" When I just change the param to zero with

let dataStr = data.base64EncodedStringWithOptions(options: 0)

It gives even stranger error: "Cannot convert the expression of type 'String!' to type 'String!'" I found a way to init NSString with NSData (however, I still can't get the difference between String and NSString), but I'm really curious why these two lines of code don't work.


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)


let dataStr = data.base64EncodedStringWithOptions(nil)


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

For Swift 2.x use an array for options:

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

For swift 3.0 use this ,

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

Swift 3.x

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

You don't have to put in the "options:" identifier in the argument. You DO have to specify that Encoding64CharacterLineLength is a static member of NSDataBase64EncodingOptions, like so:

var dataStr = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

let dataStr = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)

Since the default parameter value is set be an empty array…

/// Returns a Base-64 encoded string.
/// - parameter options: The options to use for the encoding. Default value is `[]`.
/// - returns: The Base-64 encoded string.
@inlinable public func base64EncodedString(options: Data.Base64EncodingOptions = []) -> String

you just need to call

let dataStr = data.base64EncodedString()

Need Your Help

How to sort alphabetically while ignoring case sensitive?

java android collections

I have this code, but works only for lower case letters. I want this to sort the list while ignoring the upper case letters..

Return positions of a regex match() in Javascript?

javascript regex match string-matching

Is there a way to retrieve the (starting) character positions inside a string of the results of a regex match() in Javascript?