Where can I declare constants in swiftUI?

Can I declare constants inside the VStack (or inside any other View) or is it better to declare them as properties of my struct?
The question is whether we can treat the View scope as a local scope, like, for example, the scope of a function.

As you can see, that code compiles. You should always do what you did: put constants in the most narrow scope possible.

However, you should use a ForEach instead, and move the sizing out. That will require no constants aside from the numeric literals themselves.


It makes sense. I’ve always put constants in the narrowest area when using UIKit. However, I thought that maybe in swiftUI it’s not recommended to put something other than a View in the body in order to keep it clean and readable.

You are right about ForEach. Actually, it's not a real example from the code. I was trying to show a case where I can use the same value in multiple places, keeping it as a constant instead of hard coding.

Thanks for your reply, it helped me a lot.