Reputation: 161
I have connect.swift
with code:
public struct Connect {
let adresSerwera = "http://test.nazwa.pl/"
typealias Odpowiedz = (Data?, Error?) -> Void
func getJsonFromServer(parametry: String, wynikRequesta: @escaping Odpowiedz) {
guard let Url = URL(string: self.adresSerwera + "kartyEndpoint.qbpage" + parametry) else { return }
URLSession.shared.dataTask(with: Url) { (data, response, error) in
if error == nil {
guard let data = data else {
print("Error 100: \(error)")
wynikRequesta(nil, error)
return
}
print("R>" + self.adresSerwera + "kartyEndpoint.qbpage?" + parametry)
do {
//let json = try JSONDecoder().decode(forecast.self, from: data)
wynikRequesta(data, nil)
dump(data)
print("\(data)")
} catch let err {
print("Error 101: ", err)
wynikRequesta(nil, err)
}
} else{
print("Error 102: Problem with download data")
}
}.resume()
}
func sprawdzDaneLogowania(login: String?, haslo: String?, callback: @escaping Odpowiedz) {
getJsonFromServer(parametry: "?action=LOGOWANIE&login=\(login!)&password=\(haslo!)", wynikRequesta: callback)
}
}
and code to download data:
@IBAction func btnLoginPressed(_ sender: Any) {
if self.textFieldLogin.text?.isEmpty ?? true || self.textFieldPassword.text?.isEmpty ?? true {
print("Uzupełnij wszystkie pola!!")
} else {
print("Pola uzupełnione")
cms.sprawdzDaneLogowania(login: self.textFieldLogin.text, haslo: self.textFieldLogin.text, callback: { (data, blad) in
if blad == nil{
if let dane = data {
let str = String(data: dane, encoding: .utf8)
let downloadedData = RankingGrupowyObject(JSONString: str!)
let decoder = JSONDecoder()
let zalogowanyUser = try decoder.decode(LoginUser.self, from: data)
} else {
print("Error 103: \(data)")
}
} else {
print("Error 104: \(blad)")
}
})
}
}
for lines: cms.Check the Logs (login: self.textFieldLogin.text, password: self.textFieldLogin.text, callback: {(date, error) in
I get an error message:
Invalid conversion from throwing function of '(_, _) throws -> ()' is a non-throwing function type 'Connect. Answer' (aka '(Optional , Optional ) -> ()')
What have I done wrong? How can you fix this error? By using the CheckLogging function I would like to create a target object
Upvotes: 1
Views: 489
Reputation: 1933
let zalogowanyUser = try decoder.decode(LoginUser.self, from: data)
This part is able to throw, meaning that you should either do/catch there:
do {
let zalogowanyUser = try decoder.decode(LoginUser.self, from: data)
}
catch {
print("Error in decoder")
}
Or let the error propagate onto upper parts. In order to do that, your method cms.sprawdzDaneLogowania
could be marked as throws
, or your that method's callback block could be marked as such.
Upvotes: 3