[ad_1]
I’m attempting to create objects from JSON through Codable implementation.
An object has an url pointing to an tackle for a picture. this additionally could be null within the JSON.
However attempting to encode to swift objects, imageURL
is all the time nil.
The JSON
{
"id": 1,
"title": "Buzz",
"tagline": "A Actual Bitter Expertise.",
"first_brewed": "09/2007",
"image_url": "https://photos.punkapi.com/v2/keg.png",
"abv": 4.5,
// ...
}
and
{
"id": 321,
"title": "Jinx Pale Ale",
"tagline": "American Pale Ale.",
"first_brewed": "2018",
"image_url": null,
"abv": 4.7,
// ...
}
The Beer information sort
struct Beer {
let id:Int
let title:String
let tagline:String
let abv:Double
let imageURL:URL?
}
extension Beer: Codable {}
extension Beer: Identifiable {}
extension Beer: Hashable {}
Decoding
func beersFrom(information:Knowledge) -> [Beer] {
let beers = attempt! JSONDecoder().decode([Beer].self, from: information)
return beers
}
All fields are efficiently populated, apart from the imageURL, wich all the time is nil.
I’m utilizing Xcode 15.0.1
[ad_2]