swiftUI 中的可识别协议... id 属性 ... var vs let

Identifiable protocol in swiftUI... id property... var vs let

在 Apple 的 SwiftUI Landmarks 教程中,Landmarks 结构的 id 属性 是 var 而不是 let 有什么原因吗?

import SwiftUI
import CoreLocation

struct Landmark: Hashable, Codable, Identifiable {
    var id: Int 

// ...
}

显然不是。您可以安全地将 id 转换为 let,一切都会正常工作。

其实Identifiable协议那里没有冲突。它只需要 id 即可获取。它没有提到可设置的:

public protocol Identifiable {

    /// A type representing the stable identity of the entity associated with `self`.
    associatedtype ID : Hashable

    /// The stable identity of the entity associated with `self`.
    var id: Self.ID { get }
}