var is a variable meaning it is mutable. You can reassign it a value as many time as you wish.
In LandmarkRow
and LandmarkList
, var body
is a computed property that calculates (rather than stores) a value. And it's read-only. Computed properties can only be declared using var
.
When you implement a custom view, you must implement a computed
body
property to provide the content for your view. Return a view
that's composed of primitive views that SwiftUI provides, plus other composite views that you've already defined. https://developer.apple.com/documentation/swiftui/view/body-swift.property
let
is used to declare a constant. You can assign it a value exactly once as you have done in LandmarkList
. In other words, you can not reassign it a value.
List(landmarks, id: \.id) { landmark in
LandmarkRow(landmark: landmark)
}
Example
struct Example {
// variable that you can change
var name: String
// constant that can only be assigned a value once.
let fileExtension: String
// A computed property that is read-only
var filename: String {
return name + "." + fileExtension
}
}
var example = Example(name: "example", fileExtension: "swift")
You can change name from example
to example_2
since it is a variable
.
example.name = "example_2"
You can not change fileExtension
from swift
to js
because fileExtension
is a constant (let). If you do so, you will get the following error.
Cannot assign to property: 'fileExtension' is a 'let' constant
example.fileExtension = "js"
You can not change fileName
because it is a read-only property. If you try to change, you will get this error.
Cannot assign to property: 'filename' is a get-only property
example.filename = "ex.js"
More info
What is the difference between `let` and `var` in swift?
https://docs.swift.org/swift-book/LanguageGuide/Properties.html
https://www.avanderlee.com/swift/computed-property/