所以我目前有以下内容:
class ViewController: UIViewController {
class Identity{
let baseUrl = "superSecretURL"
var _username: String = ""
var _password: String = ""
var _apiKey: String = ""
init(){
}
init(username: String, apiKey: String){
_username = username
_apiKey = apiKey
}
init(username: String, password: String){
_username = username
_password = password
}
func loginPassword() -> String{
var loginJSON = ["auth": ["passwordCredentials": ["username": _username, "password": _password]]];
var returnJSON: String
request(.POST, baseUrl, parameters: loginJSON, encoding: .JSON)
.responseJSON { (request, response, data, error) in
if let anError = error
{
// got an error in getting the data, need to handle it
println("error calling POST on /posts")
println(error)
}
else if let data: AnyObject = data
{
// handle the results as JSON, without a bunch of nested if loops
let post = JSON(data)
// to make sure it posted, print the results
println("JSON Returned")
}
}
}
}
var i = Identity(username: "secretName", password: "complicatedPassword")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("Before Call")
println("After Call")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
基本上我希望能够调用 println("Before Call") 然后从 loginPassword() 方法接收响应,然后 println("After Call")。我相信这是同步的,但我想不出一种让它工作的方法,整个线程的事情让我感到困惑。
我基本上想说:
if i.loginPassword(){ // do some login stuff }else{ // do some error stuff }
任何帮助或指针表示赞赏。