Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
i got here from javascript and now i am finding out for swift. i need to print one merchandise of my URLSession return however i dont understand how i can do that.
My code:
import Basis
import Dispatch
enum ServiceError: Error {
case invalidURL
case decodeFail(Error)
case community(Error?)
}
struct Deal with: Codable {
let zipcode: String
let tackle: String
let metropolis: String
let uf: String
let complement: String?
enum CodingKeys: String, CodingKey {
case zipcode = "cep"
case tackle = "logradouro"
case metropolis = "localidade"
case uf
case complement = "complemento"
}
}
class Service {
non-public let baseURL = "https://viacep.com.br/ws"
func get(cep: String, callback: @escaping (Outcome<Any, ServiceError>) -> Void) {
let path = "/(cep)/json"
guard let url = URL(string: baseURL + path) else {
callback(.failure(.invalidURL))
return
}
let process = URLSession.shared.dataTask(with: url) { information, response, error in
guard let information = information else {
callback(.failure(.community(error)))
return
}
guard let json: Deal with = strive? JSONDecoder().decode(Deal with.self, from: information) else {
return
}
callback(.success(json))
}
process.resume()
}
}
do {
let service = Service()
service.get(cep: "1231234") { end in
DispatchQueue.most important.async {
change consequence {
case let .failure(error):
print(error)
case let .success(information):
print(information)
}
}
}
}
That is my return:
Deal with(zipcode: “12341231”, tackle: “Teste”, metropolis: “Teste”, uf: “TE”, complement: Non-compulsory(“”))
I need need my return is like:
print(information.zipcode)
Output: 12341231
[ad_2]