Question re: 5.3 Function builder enhancement: let is allowed but not assignment

You haven't provided the whole source, or the error you get, which is why everyone is so confused. I guess it's something like this:

struct Foo {
    static var x = 0
    @ViewBuilder
    var body: some View {
        Self.x = 123 // Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
        VStack {
            Text("\(x)")
        }
    }
}

ViewBuilder expects that every expression is an instance of a View

foo = bar is an expression just like normal operators. Every expression in swift has to return something. Usually when you think that a function doesn't return anything, it returns Void instead. Just like 2+2 returns an Int, Self.x = 123 returns Void

Void is an empty tuple ()

Empty tuple is not a View which is why you get the error you get.


It seems you are trying to mix imperative paradigm with SwiftUI which is very declarative which is not a good idea. I would suggest to put your formatter into Environment instead, and change the time zone of it every time activeTimeZone changes

3 Likes