Home > Software engineering >  Identifiable id not required by reference types (class) but required by structs
Identifiable id not required by reference types (class) but required by structs

Time:07-15

I recently came across the following code:

struct Bar: Identifiable {
    // Type 'Bar' does not conform to protocol 'Identifiable'
}

class Foo: Identifiable {
   
}

For the Bar struct, Xcode complains that 'Type 'Bar' does not conform to protocol 'Identifiable' but for Foo Xcode never complains.

My understanding is that Bar is a value type and we need to supply a value for the id property to conform to the Identifiable.

Foo is reference type so its memory address is unique and thus not need to include the id conformance by Identifiable.

Is this correct?

CodePudding user response:

Not exactly, there is an explicit extension for Identifiable, which makes that possible for classes:

demo demo1

  • Related