A coworker is claiming that we should not use constants in SwiftUI View structs. Even for properties of a value type where the value will not change.
For example, they say this is not good:
struct FlagView: View {
let flagIcon = Image(systemName: "flag")
var body: some View {
VStack {
flagIcon
Text("Flags")
flagIcon
}
}
}
But do this instead:
struct FlagView: View {
var flagIcon: Image { Image(systemName: "flag") }
var body: some View {
VStack {
flagIcon
Text("Flags")
flagIcon
}
}
}
Their explanation sounds like cargo cult based on their misinterpretation of Apple tutorial code and ignorance of fundamentals. However, proving that is elusive. Apple documentation is not going to explain every crazy thing we should not do because that is an infinite list.
Or maybe there is magic in SwiftUI I am ignorant of.
Is their assertion correct? If yes or no, please explain why.
Note, I realize this is a simplistic example and really we would type the Image out twice in the VStack. I am trying to focus on the general case.


