字符串 urlsession 响应未正确处理
string urlsession response is not handled correctly
我是 swift 的初学者,我创建了 post URLSession,我想从中获取字符串响应,如果我得到“EXIST”,则表示用户存在,我会显示警报,如果我得到“确定”,则相反。
我想做的是以那种方式处理这种反应,我在操场上看到
像这样的响应“存在”或“确定”但是当我想比较它时它不起作用并且我的警报 none 被执行
//这里是我的函数
@IBAction func register(_ sender: Any) {
print("hello")
if(txtPassword.text != txtPasswordrepeat.text) {
let alert = UIAlertController(title: "Register Failed", message: "Passwords Mismatch", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
print("passwords mismatch")
}else {
//post
guard let url = URL(string: "http://localhost:3000/register/") else {
return
}
let bodyparameters = ["name": txtName.text,"lastname": txtLastname.text, "phone": txtPhone.text,"email": txtEmail.text, "password": txtPassword.text]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: bodyparameters, options: []) else{
return
}
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data,response,error) in
if let response = response {
print(response)
}
if let data = data , let dataString = String(data: data, encoding: String.Encoding.utf8){
print(data)
DispatchQueue.main.async {
let string = dataString as! String
print(string)
if(string.elementsEqual("EXIST") == true ){
let alert = UIAlertController(title: "Register Failed", message: "User Already Exist", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}else if(string.elementsEqual("OK") == true){
let alert = UIAlertController(title: "Register Successful", message: "User Registred", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
}
}.resume()
}
}
//播放地面响应
2020-12-12 14:06:55.285299-0800 Bicycall[25347:386347] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
<NSHTTPURLResponse: 0x6000036f0140> { URL: http://localhost:3000/login } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
74
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:06:55 GMT"
);
Etag = (
"W/\"4a-yeGoMwACtIYJqSJyS6bmKwCePsk\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
74 bytes
21
INSERT SUCCESSFULLY
21
hello
<NSHTTPURLResponse: 0x6000036fa6c0> { URL: http://localhost:3000/register/ } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
7
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:07:04 GMT"
);
Etag = (
"W/\"7-cf+VCoacHzeb2ZclL0nsiKzxBq4\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
7 bytes
"EXIST"
hello
<NSHTTPURLResponse: 0x6000036e7bc0> { URL: http://localhost:3000/register/ } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
7
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:07:06 GMT"
);
Etag = (
"W/\"7-cf+VCoacHzeb2ZclL0nsiKzxBq4\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
7 bytes
"EXIST"
hello
<NSHTTPURLResponse: 0x6000036ebcc0> { URL: http://localhost:3000/register/ } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
7
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:07:07 GMT"
);
Etag = (
"W/\"7-cf+VCoacHzeb2ZclL0nsiKzxBq4\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
7 bytes
"EXIST"
我想知道我在这里遗漏了什么
欢迎来到 SO 和 Swift!
您的服务器正在发送 "EXIST"
,但您正在检查 EXIST
。
IOW,而不是 string.elementsEqual("EXIST") == true
,试试 string.elementsEqual("\"EXIST\"") == true
。
但不要就此止步。将表达式简化为 string == "\"EXIST\""
.
我是 swift 的初学者,我创建了 post URLSession,我想从中获取字符串响应,如果我得到“EXIST”,则表示用户存在,我会显示警报,如果我得到“确定”,则相反。 我想做的是以那种方式处理这种反应,我在操场上看到 像这样的响应“存在”或“确定”但是当我想比较它时它不起作用并且我的警报 none 被执行
//这里是我的函数
@IBAction func register(_ sender: Any) {
print("hello")
if(txtPassword.text != txtPasswordrepeat.text) {
let alert = UIAlertController(title: "Register Failed", message: "Passwords Mismatch", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
print("passwords mismatch")
}else {
//post
guard let url = URL(string: "http://localhost:3000/register/") else {
return
}
let bodyparameters = ["name": txtName.text,"lastname": txtLastname.text, "phone": txtPhone.text,"email": txtEmail.text, "password": txtPassword.text]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: bodyparameters, options: []) else{
return
}
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data,response,error) in
if let response = response {
print(response)
}
if let data = data , let dataString = String(data: data, encoding: String.Encoding.utf8){
print(data)
DispatchQueue.main.async {
let string = dataString as! String
print(string)
if(string.elementsEqual("EXIST") == true ){
let alert = UIAlertController(title: "Register Failed", message: "User Already Exist", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}else if(string.elementsEqual("OK") == true){
let alert = UIAlertController(title: "Register Successful", message: "User Registred", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
}
}.resume()
}
}
//播放地面响应
2020-12-12 14:06:55.285299-0800 Bicycall[25347:386347] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
<NSHTTPURLResponse: 0x6000036f0140> { URL: http://localhost:3000/login } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
74
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:06:55 GMT"
);
Etag = (
"W/\"4a-yeGoMwACtIYJqSJyS6bmKwCePsk\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
74 bytes
21
INSERT SUCCESSFULLY
21
hello
<NSHTTPURLResponse: 0x6000036fa6c0> { URL: http://localhost:3000/register/ } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
7
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:07:04 GMT"
);
Etag = (
"W/\"7-cf+VCoacHzeb2ZclL0nsiKzxBq4\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
7 bytes
"EXIST"
hello
<NSHTTPURLResponse: 0x6000036e7bc0> { URL: http://localhost:3000/register/ } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
7
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:07:06 GMT"
);
Etag = (
"W/\"7-cf+VCoacHzeb2ZclL0nsiKzxBq4\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
7 bytes
"EXIST"
hello
<NSHTTPURLResponse: 0x6000036ebcc0> { URL: http://localhost:3000/register/ } { Status Code: 200, Headers {
Connection = (
"keep-alive"
);
"Content-Length" = (
7
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sat, 12 Dec 2020 22:07:07 GMT"
);
Etag = (
"W/\"7-cf+VCoacHzeb2ZclL0nsiKzxBq4\""
);
"Keep-Alive" = (
"timeout=5"
);
"X-Powered-By" = (
Express
);
} }
7 bytes
"EXIST"
我想知道我在这里遗漏了什么
欢迎来到 SO 和 Swift!
您的服务器正在发送 "EXIST"
,但您正在检查 EXIST
。
IOW,而不是 string.elementsEqual("EXIST") == true
,试试 string.elementsEqual("\"EXIST\"") == true
。
但不要就此止步。将表达式简化为 string == "\"EXIST\""
.