Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swiftui

In the Landmarks tutorial for SwuiftUI by Apple, is there some reason why the id property of the Landmarks struct is a var and not a let?

import SwiftUI
import CoreLocation

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

// ...
}
like image 967
Nerdy Bunz Avatar asked Sep 17 '25 00:09

Nerdy Bunz


1 Answers

Apparently not. You can safely turn the id into a let and all will work fine.

In fact, the Identifiable protocol has no conflict there. It only requires id to be gettable. It says nothing about settable:

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 }
}
like image 145
kontiki Avatar answered Sep 19 '25 22:09

kontiki