UUID, or not UUID?

hejliang

Liang Wang

Posted on January 29, 2024

UUID, or not UUID?

I have my own JSON file hosted on Github page

Basically, it contains two value pairs: name (string) and elements (array of Element)

{"name": "Marathon Training Phase",
"elements": [
{
"title": "Base Building",
"headline": "Always lay the foundation first.",
"image": "base"
},]
}

I want to load and decode the data from it by using an APIService like this:

do {
let url = URL(string: baseURL)!
let (data, _) = try await session.data(from: url)
return try JSONDecoder().decode([Marathon].self, from: data)
} catch {
fatalError("Failed to load data: \(error.localizedDescription))
}

Clearly in the JSON file there is no UUID, just name and elements. That's it. There really is no need to add var id = UUID() to the struct model, or conform to Identifiable protocol when create the struct. And for me I happened to add it and then got problems when decoding that JSON because there is no value of id to be decoded, and Xcode will complain loudly.

💖 💪 🙅 🚩
hejliang
Liang Wang

Posted on January 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related