斯威夫特2:Alamofire GET问题

时间:2016-05-05 03:33:07

标签: swift2 xcode7 alamofire

我尝试执行GET请求但遇到以下两个错误:

  

使用未解析的标识符' self'

  

预期声明

这是我受影响的代码

let credentialData = "\(self._username):\(self._password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.GET, "http://gemcave.pythonanywhere.com/api/order\(self._username)/\(self._password)", headers: headers)
.responseJSON { response in
debugPrint(response)
}

Errors encountered

1 个答案:

答案 0 :(得分:1)

您需要将代码放入以下方法:

func networkConfiguration() {
  let credentialData = "\(self._username):\(self._password)".dataUsingEncoding(NSUTF8StringEncoding)!
  let base64Credentials = credentialData.base64EncodedStringWithOptions([])
  let headers = ["Authorization": "Basic \(base64Credentials)"]
  Alamofire.request(.GET, "http://gemcave.pythonanywhere.com/api/order\(self._username)/\(self._password)", headers: headers)
  .responseJSON { response in
  debugPrint(response)
  }
}