如何将以下代码从 Swift 2 翻译成 Swift 5?
How can I translate the following code from Swift 2 to Swift 5?
相信下面的代码是写在Swift 2.如何把语法转换成最新的Swift(5)?
当使用 Xcode 进行转换时,它给我留下了如下错误:
Extra argument 'usingEncoding' in call
和
Cannot call value of non-function type 'URLSession'
原件(需要帮助转换):
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.sample.com/sample.php")!)
request.HTTPMethod = "POST"
let postString = "a=\(customerLabel!)"
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 responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
}
这是我的尝试,但有错误:
let request = NSMutableURLRequest(url: URL(string: "http://www.sample.com/sample.php")!)
request.httpMethod = "POST"
let postString = "a=\(customerLabel!)"
request.HTTPBody = postString.data(usingEncoding: NSUTF8StringEncoding)
let task = URLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
- 不要使用
NSMutableURLRequest
。使用 URLRequest
.
- 不要使用
NSString
,使用String
。
- 查看
URLSession
文档,发现您需要 shared
,而不是 sharedInstance()
。
data(using .utf8)
.
- 许多其他修复。
这是您的固定代码,可以更好地处理完成处理程序中的可选值:
var request = URLRequest(url: URL(string: "http://www.sample.com/sample.php")!)
request.httpMethod = "POST"
let postString = "a=\(customerLabel!)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("error=\(error)")
return
}
print("response = \(response)")
if let data = data, let responseString = String(data: data, encoding: .utf8) {
print("responseString = \(responseString)")
}
}
task.resume()
相信下面的代码是写在Swift 2.如何把语法转换成最新的Swift(5)?
当使用 Xcode 进行转换时,它给我留下了如下错误:
Extra argument 'usingEncoding' in call
和
Cannot call value of non-function type 'URLSession'
原件(需要帮助转换):
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.sample.com/sample.php")!)
request.HTTPMethod = "POST"
let postString = "a=\(customerLabel!)"
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 responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
}
这是我的尝试,但有错误:
let request = NSMutableURLRequest(url: URL(string: "http://www.sample.com/sample.php")!)
request.httpMethod = "POST"
let postString = "a=\(customerLabel!)"
request.HTTPBody = postString.data(usingEncoding: NSUTF8StringEncoding)
let task = URLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
- 不要使用
NSMutableURLRequest
。使用URLRequest
. - 不要使用
NSString
,使用String
。 - 查看
URLSession
文档,发现您需要shared
,而不是sharedInstance()
。 data(using .utf8)
.- 许多其他修复。
这是您的固定代码,可以更好地处理完成处理程序中的可选值:
var request = URLRequest(url: URL(string: "http://www.sample.com/sample.php")!)
request.httpMethod = "POST"
let postString = "a=\(customerLabel!)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("error=\(error)")
return
}
print("response = \(response)")
if let data = data, let responseString = String(data: data, encoding: .utf8) {
print("responseString = \(responseString)")
}
}
task.resume()