- У меня вопрос по поводу
let jsonResponse = try JSONSerialization.jsonObject (with: dataResponse, options: .allowFragments)
print ("jsonResponse ==> (jsonResponse)")
Я хочу изменить значение
jsonResponse ==> jsonArray, но значение не отправляется в jsonArray
jsonArray ==> jsonDictionary
И используйте словарь JSON для проверки входа в систему далее ...
- У меня есть больше ошибок
guard let jsonDictionary: Dictionary = jsonArray [0] else {==> Have warning = Non-optional expression of type '[String: Any]' used in a check for optionals
return
} // guard
print ("jsonDictionary ==> (jsonDictionary)")
если я изменюсь
- JSONSerialization.jsonObject (with: dataResponse, options: [])
есть ошибка "JSON-текст не начинался с массива или объекта и опция, позволяющая не устанавливать фрагменты."
// function check Authen
func checkAuthen(truckNo: String, pass: String, imei: String) -> Void {
let myconstant = Myconstant()
let urlcheckLogin = myconstant.jsonGetDataCheckAuthen(pTruckNo: truckNo, pPassword: pass, pIMEI: imei)
print("urlcheckLogin ==> \(urlcheckLogin)")
guard let url = URL(string: urlcheckLogin) else {
return
}//Guard
// task
let task = URLSession.shared.dataTask(with: url){ (data, response, error) in
guard let dataResponse = data, error == nil else{
print("Have Error")
return
}//guard
do{
// read json from API
let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse, options: .allowFragments)
print("jsonResponse ==> \(jsonResponse)")
// change json to array
guard let jsonArray = jsonResponse as? [[String:Any]] else{
return
}//guard
print("jsonArray ==> \(jsonArray)")
guard let jsonDictionary: Dictionary = jsonArray[0] else{
return
}//guard
print("jsonDictionary ==>\(jsonDictionary)")
// check true password for json dictionary
let truePassword: String = jsonDictionary["Password"] as! String
print("truePassword ==>\(truePassword)")
if pass == truePassword{
// password incorrect
DispatchQueue.main.async {
self.performSegue(withIdentifier: "GotoPlanData", sender: self)
}//DispatchQueue
}else{
// password incorrect
self.showAlert(title: "Password incorrect", message: "Plase try again")
}//if
}catch let myerror{
print(myerror)
// check display username in database
print("No have user \(truckNo) in database")
DispatchQueue.main.async {
self.showAlert(title: "No username", message: "No have user \(truckNo) in database")
}//DispatchQueue
}//catch
}//task
task.resume()
}//checkAuthen
Результат
No Space
urlcheckLogin ==> http://www.testservice.com/Service/CheckUserLogin/60-7625/1234/355750067867310
username ==> Optional("60-7625")
password ==> Optional("1234")
jsonResponse ==> [{ "Result": "Valid", "TruckNo": "60-7625", "TruckID": "10" }]