let id = UUID() compiler warning

I have model object that conform to Identifiable and Codable. In order to adhere to this conformance I set the id property to a UUID() return. In Swift 5.3 the compiler displays a warning that the id property cannot be decoded. How would I remove this warning if my model does not contain an identifier?
Try making it mutable as suggested. If you do not want to make it publicly rewritable, you can add private(set)
Code Block
    private(set) var id = UUID()

Thanks. If I make it mutable Decodable breaks because the API does not contain an "id" in the return. If there are other suggestions please let me know.

the API does not contain an "id" in the return.

You should better have included such big difference than the usual behavior of Codable.
When you define a Codable struct, encoded result does contain "id" with its default implementation.

You may need to write your customized implementation of Codable if you do not like the default implementation.
An example.
Code Block
struct MyStruct: Codable, Identifiable {
    let id = UUID()
    var name: String
    //... other properties
    enum CodingKeys: String, CodingKey {
        case name
        //... deine all keys except `id`
    }
}

But the encoded data is different than old struct using default Codable.

Do you need encoded data with "id"?
There was no compiler warning in iOS 13 and Xcode 11. The "id" is needed for conformance to Identifiable. I'm using the Codable struct in a Swift UI List so Identifiable is needed. Making it mutable breaks the decoding. I may need to move the "id" up to the ViewModel struct and out of the Model struct. Again, it is only a warning, but slightly annoying.

If you do not have an "id" coming from a JSON payload that needs to be decoded, how do you conform to Identifiable?

There was no compiler warning in iOS 13 and Xcode 11.

That does not mean your code was ok in iOS 13, just that old Swift could not detect such potentially-problematic code.

If you do not have an "id" coming from a JSON payload that needs to be decoded, how do you conform to Identifiable?

I have already shown one way.

There may be some other ways depending on your actual JSON.
I separated the Codable properties from the one used for Identifiable and it works well:
Code Block
struct Shortcut: Hashable, Codable {
   
  var text: String
  var url: String
   
  enum CodingKeys: String, CodingKey {
    case text
    case url
  }
   
}
extension Shortcut: Identifiable {
  var id: UUID { return UUID() }
}

I can't believe this wasn't considered.

The warning needs to be removed, and could be added to something like SwiftLint.
Maybe a better solution might be a keyword like @CodeableIgnore we could use.

Shouldn't have to handwrite codable code for a single ID property... that's now a requirement.

If you're testing your code, you should never run into the issues this warning is out to solve anyway.

The warning needs to be removed

DevForums is primarily focused on what is rather than what should be. Fortunately, in the case of Swift you have the opportunity to influence the language’s development via the Swift Evolution process.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

let id = UUID() compiler warning
 
 
Q