struct Test: Identifiable vs class Test: Identifiable

前端 未结 1 1887
余生分开走
余生分开走 2021-01-27 00:52
struct Test: Identifiable {}

causes the error: \"Type \'Test\' does not conform to protocol \'Identifiable\'\".
It wants \"id\" property.



        
相关标签:
1条回答
  • 2021-01-27 01:08

    From SE-0261 Identifiable Protocol (emphasis mine):

    In order to make it as convenient as possible to conform to Identifiable, a default id is provided for all class instances:

    extension Identifiable where Self: AnyObject {
        var id: ObjectIdentifier {
            return ObjectIdentifier(self)
        }
    }
    

    Then, a class whose instances are identified by their object identities need not explicitly provide an id:

    final class Contact: Identifiable {
        var name: String
    
        init(name: String) {
            self.name = name
        }
    }
    
    0 讨论(0)
提交回复
热议问题