gpt4 book ai didi

Swift UIAlert - 从 dataTaskWithRequest 获取数据

转载 作者:行者123 更新时间:2023-11-28 12:48:04 25 4
gpt4 key购买 nike

我需要从“dataTaskWithRequest”中获取变量“response”。现在,找不到“response”,因为它在括号之外。我怎样才能确保将响应变量传递给 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: {});
}
}
}

最佳答案

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: {});
}
}
}

关于Swift UIAlert - 从 dataTaskWithRequest 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37403024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com