Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating an Identifiable id gives inmutable warning

I have the following:

struct myModel: Decodable, Identifiable {
    let id = UUID()
    let a, b, c: String
}

I’m getting the following warning:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Immutable property will not be decoded because it is declared with an initial value which cannot be overwritten

I don’t want to change the id once the data is refreshed, I just want to assign it once. How can I get rid of that warning?

>Solution :

Either declare id as var or add CodingKeys and omit id.

And please name structs and classes always with starting capital letter

struct MyModel: Decodable, Identifiable {
    private enum CodingKeys: String, CodingKey { case a, b, c } 

    let id = UUID()
    let a, b, c: String
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading