jskidd3 jskidd3 - 11 months ago 74
iOS Question

Swift - base64EncodingStringWithOptions not working Xcode 6.1

let username = "operator"
let password = "pass"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromMask(0))

// create the request
let url = NSURL(string: "myurl")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")

The above code was working fine pre-Xcode 6.1 but now I get an error on the

/Users/Joel/Project/MapViewController.swift:31:74: 'NSDataBase64EncodingOptions.Type' does not have a member named 'fromMask'

I am relatively new to Swift and would like to also ask some advice as to how I should go about solving this sort of issue without having to post to StackOverflow.

Answer Source

The fromMask() constructor does not exist anymore in the current version of Swift. To specify "no option", use

let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(0))

or simply

let base64LoginString = loginData.base64EncodedStringWithOptions(nil)

For Swift 2 (compare http://stackoverflow.com/a/32499968/1187415):

let base64LoginString = loginData.base64EncodedStringWithOptions([])

For Swift 3:

let base64LoginString = loginData.base64EncodedString()