Hi there! I have a public structure and I called it in my ContentView, but I can't understand where to punt the var/let. I try to put them before "var body: some view" but it gives me these errors.
Where to put variables/constants
I would write either:
public struct Block: View {
let size : CGFloat = 0 // or var
let color: Color = .red // or var
public init() { // init not needed
}
public var body : some View {
Text("Hello")
}
}
or
public struct Block: View {
let size : CGFloat // or var
let color: Color // or var
public init() {
size = 0
color = .red
}
public var body : some View {
Text("Hello")
}
}
Note that a constant (let) cannot be modified but can be set in init(), if not set before.
So, this fails:
public struct Block: View {
let size : CGFloat = 0
let color: Color = .red
public init() {
size = 0 // error Immutable value 'self.size' may only be initialized once
color = .red // error Immutable value 'self.color' may only be initialized once
}
public var body : some View {
Text("Hello")
}
}