DEV Community

Liang Wang
Liang Wang

Posted on • Edited on

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.

Top comments (0)