我需要变量'响应'取自" dataTaskWithRequest"。现在,'回复'找不到,因为它在括号之外。我怎样才能确保将响应变量传递给UIAlert?感谢
这是我的代码:
@IBAction func buttonCreateAccount(sender: AnyObject) {
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.example.com/createaccount.php")!)
request.HTTPMethod = "POST"
let postString = "user_name=\(username.text!)&email=\(email.text!)&password=\(password.text!)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let response = String(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(response)")
}
task.resume()
//problem is here. 'response' variable cannot be taken from above. i need it to be taken from above.
if response == "Username taken" {
if let getModernAlert: AnyClass = NSClassFromString("UIAlertController") { // iOS 8
let myAlert: UIAlertController = UIAlertController(title: "Registration", message: response, preferredStyle: .Alert)
myAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(myAlert, animated: true, completion: nil)
} else { // iOS 7
let alert: UIAlertView = UIAlertView()
alert.delegate = self
alert.title = "Registration"
alert.message = "Testing"
alert.addButtonWithTitle("OK")
alert.show()
}
} else {
if let getModernAlert: AnyClass = NSClassFromString("UIAlertController") { // iOS 8
let myAlert: UIAlertController = UIAlertController(title: "Registration", message: response, preferredStyle: .Alert)
myAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(myAlert, animated: true, completion: nil)
} else { // iOS 7
let alert: UIAlertView = UIAlertView()
alert.delegate = self
alert.title = "Registration"
alert.message = "Testing"
alert.addButtonWithTitle("OK")
alert.show()
self.dismissViewControllerAnimated(true, completion: {});
}
}
}
答案 0 :(得分:2)
dataTaskWithRequest
以异步运行。你不会立即得到答复。将其分为两个功能:
@IBAction func buttonCreateAccount(sender: AnyObject) {
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.example.com/createaccount.php")!)
request.HTTPMethod = "POST"
let postString = "user_name=\(username.text!)&email=\(email.text!)&password=\(password.text!)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let response = String(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(response)")
// Now that the response is ready, call the other function to handle it
handleResponse(response)
}
task.resume()
}
func handleResponse(response: String) {
if response == "Username taken" {
if let getModernAlert: AnyClass = NSClassFromString("UIAlertController") { // iOS 8
let myAlert: UIAlertController = UIAlertController(title: "Registration", message: response, preferredStyle: .Alert)
myAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(myAlert, animated: true, completion: nil)
} else { // iOS 7
let alert: UIAlertView = UIAlertView()
alert.delegate = self
alert.title = "Registration"
alert.message = "Testing"
alert.addButtonWithTitle("OK")
alert.show()
}
} else {
if let getModernAlert: AnyClass = NSClassFromString("UIAlertController") { // iOS 8
let myAlert: UIAlertController = UIAlertController(title: "Registration", message: response, preferredStyle: .Alert)
myAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(myAlert, animated: true, completion: nil)
} else { // iOS 7
let alert: UIAlertView = UIAlertView()
alert.delegate = self
alert.title = "Registration"
alert.message = "Testing"
alert.addButtonWithTitle("OK")
alert.show()
self.dismissViewControllerAnimated(true, completion: {});
}
}
}