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")
上面的代码在Xcode 6.1之前运行良好,但现在我在base64LoginString
常量上出错了。
/Users/Joel/Project/MapViewController.swift:31:74: 'NSDataBase64EncodingOptions.Type' does not have a member named 'fromMask'
我对Swift相对较新,并且还想就如何解决此类问题提出一些建议,而不必发布到StackOverflow。
答案 0 :(得分:3)
当前版本的Swift中不再存在fromMask()
构造函数。
要指定"无选项",请使用
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(0))
或只是
let base64LoginString = loginData.base64EncodedStringWithOptions(nil)
对于 Swift 2 (比较https://stackoverflow.com/a/32499968/1187415):
let base64LoginString = loginData.base64EncodedStringWithOptions([])
Swift 3:
let base64LoginString = loginData.base64EncodedString()