ios – easy methods to not get empty array in swift playground?

[ad_1]

I am getting response from API’s however I’m not capable of get emails array again, what am I doing fallacious?
I attempted so as to add DispatchQueue.important.asyn { emails.append(end result.information.e mail) } however its the identical end result.

// MARK: - Response Class
struct Response: Codable {
    let information: DataClass
    let assist: Help
}

// MARK: - DataClass
struct DataClass: Codable {
    let id: Int
    let e mail, firstName, lastName: String
    let avatar: String

    enum CodingKeys: String, CodingKey {
        case id, e mail
        case firstName = "first_name"
        case lastName = "last_name"
        case avatar
    }
}

// MARK: - Help
struct Help: Codable {
    let url: String
    let textual content: String
}

let urls = [
    URL(string: "https://reqres.in/api/users/1"),
    URL(string: "https://reqres.in/api/users/3"),
    URL(string: "https://reqres.in/api/users/10")
]


func getEmailFromAPI(urls: [URL?]) -> [String] {
    var emails: [String] = []
    for url in urls {
        URLSession.shared.dataTask(with: URLRequest(url: url!)) { information, response, error in
            guard let information = information else { return }
            guard let end result = strive? JSONDecoder().decode(Response.self, from: information) else { return }
            print(end result.information.e mail)
            emails.append(end result.information.e mail)
        }.resume()
    }
    return emails
}

print(getEmailFromAPI(urls: urls))

Please recommendation as to what am I doing fallacious, Thanks.

[ad_2]

Leave a Reply