为 POST 请求使用辅助函数
Using A Helper Function for POST Request
我尝试为我的 POST 请求创建一个助手 class,并希望 return 响应。但是,由于 post 请求是异步的,这让我感到有点困惑。
我试过 returning NSString 但是它没有让我 return response
和 responseString
。它只是让我放return "A"
。我尝试使用 -> NSURLResponse
但也无法正常工作。
制作这样的辅助方法的正确方法是什么? (如果我得到响应后进行检查,并且根据响应return true 或 false 也可以)
class func hello(name: String) -> NSString {
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.thisismylink.com/postName.php")!)
request.HTTPMethod = "POST"
let postString = "Hi, \(name)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
return "A"
}
由于 dataTaskWithRequest
是异步的,该函数将在执行完成块之前命中您的 return 语句。你应该做的是为你自己的辅助方法设置一个完成块,或者将某种委托对象传递给函数,这样你就可以在它上面调用一个方法,让它知道 web 服务回调的结果是什么。
这是一个使用完成块的例子:
class func hello(name: String, completion: (String? -> Void)){
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.thisismylink.com/postName.php")!)
request.HTTPMethod = "POST"
let postString = "Hi, \(name)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
completion(responseString);
}
task.resume()
}
然后使用它
<#YourClass#>.hello("name") { responseString in
//do something with responseString
}
尚未测试代码,但应该是正确的
我尝试为我的 POST 请求创建一个助手 class,并希望 return 响应。但是,由于 post 请求是异步的,这让我感到有点困惑。
我试过 returning NSString 但是它没有让我 return response
和 responseString
。它只是让我放return "A"
。我尝试使用 -> NSURLResponse
但也无法正常工作。
制作这样的辅助方法的正确方法是什么? (如果我得到响应后进行检查,并且根据响应return true 或 false 也可以)
class func hello(name: String) -> NSString {
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.thisismylink.com/postName.php")!)
request.HTTPMethod = "POST"
let postString = "Hi, \(name)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
return "A"
}
由于 dataTaskWithRequest
是异步的,该函数将在执行完成块之前命中您的 return 语句。你应该做的是为你自己的辅助方法设置一个完成块,或者将某种委托对象传递给函数,这样你就可以在它上面调用一个方法,让它知道 web 服务回调的结果是什么。
这是一个使用完成块的例子:
class func hello(name: String, completion: (String? -> Void)){
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.thisismylink.com/postName.php")!)
request.HTTPMethod = "POST"
let postString = "Hi, \(name)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
completion(responseString);
}
task.resume()
}
然后使用它
<#YourClass#>.hello("name") { responseString in
//do something with responseString
}
尚未测试代码,但应该是正确的