I have a pet peeve against the misuse of "id" to mean "ID." Unfortunately, the Identifiable protocol requires a hard-coded member called id for conformance; fortunately you can use a computed property for it. But doing so creates a perplexing complaint from the compiler:
struct BMessage: Codable, Identifiable
{
var id: String // Misspelling of "ID" required for Identifiable protocol
{
get
{
return ID // "Ambiguous use of ID"
}
}
var ID: String = ""
...
init()
{
ID = UUID().uuidString // "Ambiguous use of ID"
}
...
}
To get around this, I have to put self.ID all over the place. Why?
The
Identifierprotocol declares an associated type namedID. Your struct'sIDproperty name is causing a conflict and resulting in the ambiguous name error.Property names should start with lowercase letters so simply using
idinstead of trying to useIDmakes more sense any way. If you were trying to name the property asIDdue to JSON parsing, you can useCodingKeysto map theIDJSON key to theidstruct property.