Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifiable id not required by reference types (class) but required by structs

Tags:

swift

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?

like image 831
Mary Doe Avatar asked Nov 28 '25 18:11

Mary Doe


1 Answers

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

demo demo1

like image 172
Asperi Avatar answered Nov 30 '25 10:11

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!