Last active
February 5, 2022 03:46
-
-
Save robertmryan/6eeb9b1b94cc1392457c2017a07dd1ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct HoundResponse<T: Decodable>: Decodable { | |
var message: T? | |
var status: String | |
} | |
let url = URL(string: "https://dog.ceo/api/breed/hound/images")! | |
URLSession.shared.dataTask(with: url) { data, _, error in | |
guard let data = data, error == nil else { | |
print(error ?? URLError(.badServerResponse)) | |
return | |
} | |
do { | |
let result = try JSONDecoder().decode(HoundResponse<[URL]>.self, from: data) | |
print(result) | |
} catch let parseError { | |
print(parseError) | |
} | |
}.resume() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or, if you wanted a single random image:
Note, I added
/random
to the end of the URL, but usedResponse<URL>
(not an array).