gpt4 book ai didi

swift - Swift 中的可识别协议(protocol) : class vs struct

转载 作者:行者123 更新时间:2023-12-05 01:35:46 25 4
gpt4 key购买 nike

<分区>

我正在使用 Swift 5.3

试图理解为什么当我声明这个构造时

final class MyActivity: Identifiable {
public let iHaveNoId: String = ""
}

它编译没有任何错误(即使我没有实现“id”字段),而对于结构

struct MyActivity: Identifiable {
public let iHaveNoId: String = ""
}

我收到一个错误(正如预期的那样)- Type 'MyActivity' does not conform to protocol 'Identifiable'

此外,如果我复制可识别的源代码并将其重命名为我自己的名字,例如

public protocol MyIdentifiable {
associatedtype ID : Hashable
var id: Self.ID { get }
}

那么实现 MyIdentifiable 协议(protocol)的结构和类都将失败并出现正确的错误 Type 'MyActivity' does not conform to protocol 'MytIdentifiable'

我很困惑。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com