ios – How can entry one merchandise of struct in swift

[ad_1]

i got here from javascript and now i am learning 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 handle: String
    let metropolis: String
    let uf: String
    let complement: String?
    
    enum CodingKeys: String, CodingKey {
        case zipcode = "cep"
        case handle = "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") { lead to
        DispatchQueue.essential.async {
            change end result {
            case let .failure(error):
                print(error)
            case let .success(information):
                print(information)
            }
        }
    }

}

That is my return:

Deal with(zipcode: “12341231”, handle: “Teste”, metropolis: “Teste”, uf: “TE”, complement: Elective(“”))

I would like need my return is like:

print(information.zipcode)

Output: 12341231

[ad_2]

Leave a Reply