【发布时间】:2016-08-04 04:24:49
【问题描述】:
我正在使用 XCode 7.3。这是我的代码:
func postToServerFunction() {
let url: NSURL = NSURL(string: "http://mytesturl.com")!
let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
//let textfeld = String(UserTextField.text)
let bodyData = "data=" + UserTextField.text!
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in}}
这很好用,我的 php-Script 得到了它应该得到的字符串。但是有
sendAsynchronousRequest 在 iOS 9 中已弃用
信息。
正如我所读到的,使用 Swift 2 的方法发生了变化。
我为这个错误搜索了很多,但我无法将它匹配的代码转换为 Swift 2。正如我也读到的,我应该使用类似这样的东西
let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request)
但我无法理解。实际上,我并不理解每一行。我自己编写了代码,但从一些例子来看它对我有用。
【问题讨论】:
-
正如我所说,我知道你已经链接的帖子。但我无法用这个例子来转换它。我不知道我必须更换什么,而不是女巫线。也许有人可以解释一下。谢谢。
-
与其自己设置 NSURLSession,我更愿意使用 Alamofire (github.com/Alamofire/Alamofire),它更优雅、更易于使用。
-
使用Alamofire @t0day 说的。
标签: ios swift2 nsurlconnection deprecated nsurl