swift iOS 从异步获取值(post 数据)
swift iOS Get value from async (post data)
我在异步过程中获取值时遇到问题。
我的 class 是:
public class PostService: NSObject {
var result : String = String()
//this is my function
func request(var dataPost : String, var destination : String)->String{
let request = NSMutableURLRequest(URL: NSURL(string: destination as String)!)
request.HTTPMethod = "POST"
let postString = dataPost
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
self.result = responseString as String!
}
task.resume()
//problem is here that result will fill after async
return self.result
}
}
// 我在这里使用这个函数
override func viewDidLoad() {
super.viewDidLoad()
var dataPost :String="Name=Ali"
var dest : String = "http://example.com/api/getUserName"
var result = Post.request(dataPost, destination: dest)
lbltext.text = result as String!
}
错误的方法
override func viewDidLoad() {
var result = Post.request(dataPost, destination: dest)// This is the method requesting web data from server asynchronously
lbltext.text = result as String! // This is wrong, how can you expect data on the very next line , it will always be nil
你所做的,调用一个委托方法并将数据传递给你的视图控制器class,一旦在你的func request
中完成下载,然后从中填充数据你的委托方法。
func request(var dataPost : String, var destination : String, successHandler: (response: String) -> Void)->String{
let request = NSMutableURLRequest(URL: NSURL(string: destination as String)!)
request.HTTPMethod = "POST"
let postString = dataPost
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
//self.result = responseString as String!
successHandler(responseString as String!);
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
var dataPost :String="Name=Ali"
var dest : String = "http://example.com/api/getUserName"
Post.request(dataPost, destination: dest, successHandler: {
(response) in
lbltext.text = response;
});
}
类似下面的内容。基本上,您传递成功处理程序的回调并在回调函数内执行操作。
异步操作意味着,它们独立于代码的字节序列发生。这就是为什么当你进行异步调用时,你不能 return 一个值。代码的原始序列 运行s 而异步调用 运行s 在后台(或在另一个线程中)。这就是存在成功和错误处理程序的原因。它们允许您在异步请求完成时 运行 一段代码。
P.S请重新格式化您的问题并确保所有代码都正确缩进。
我在异步过程中获取值时遇到问题。
我的 class 是:
public class PostService: NSObject {
var result : String = String()
//this is my function
func request(var dataPost : String, var destination : String)->String{
let request = NSMutableURLRequest(URL: NSURL(string: destination as String)!)
request.HTTPMethod = "POST"
let postString = dataPost
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
self.result = responseString as String!
}
task.resume()
//problem is here that result will fill after async
return self.result
}
}
// 我在这里使用这个函数
override func viewDidLoad() {
super.viewDidLoad()
var dataPost :String="Name=Ali"
var dest : String = "http://example.com/api/getUserName"
var result = Post.request(dataPost, destination: dest)
lbltext.text = result as String!
}
错误的方法
override func viewDidLoad() {
var result = Post.request(dataPost, destination: dest)// This is the method requesting web data from server asynchronously
lbltext.text = result as String! // This is wrong, how can you expect data on the very next line , it will always be nil
你所做的,调用一个委托方法并将数据传递给你的视图控制器class,一旦在你的func request
中完成下载,然后从中填充数据你的委托方法。
func request(var dataPost : String, var destination : String, successHandler: (response: String) -> Void)->String{
let request = NSMutableURLRequest(URL: NSURL(string: destination as String)!)
request.HTTPMethod = "POST"
let postString = dataPost
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
//self.result = responseString as String!
successHandler(responseString as String!);
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
var dataPost :String="Name=Ali"
var dest : String = "http://example.com/api/getUserName"
Post.request(dataPost, destination: dest, successHandler: {
(response) in
lbltext.text = response;
});
}
类似下面的内容。基本上,您传递成功处理程序的回调并在回调函数内执行操作。
异步操作意味着,它们独立于代码的字节序列发生。这就是为什么当你进行异步调用时,你不能 return 一个值。代码的原始序列 运行s 而异步调用 运行s 在后台(或在另一个线程中)。这就是存在成功和错误处理程序的原因。它们允许您在异步请求完成时 运行 一段代码。
P.S请重新格式化您的问题并确保所有代码都正确缩进。