Learn Courses My Dashboard

Do some .modifiers and keywords have to come first?

I’m doing Module 4 Lesson 8 challenge and as errors come up I try to solve them on my own or google it but become defeated and check Chris’s answer and a simple flip in the order of things works. Is there some way to know if certain things have to come first?

Such as in this lesson’s challenge I put the .fill modifier after the .frame modifier and it kept tell me ‘someView’ has no member ‘fill’. So I’m checking Chris’s answer and he put the .fill keyword first. I try switching my code to have .fill first and it worked. Then I try copying the code from Chris’s answer to test with and come to the same conclusion.

Additionally when writing this challenge I put the LazyVStack inside the ForEach loop and it isn’t working. When I check Chris’s answer he has the ForEach inside the LazyVStack. I get that this is a container element and but to a beginner such as myself I don’t understand why the other way around wouldn’t work. P.S. I realize obvious keywords have to come before others where it affects the design.

Needless to say is their general rules to figure out the less obvious order of code?


order switched

Chris’s .fill code


order switched

Most modifiers are implemented as methods on View. Some are methods on more specialized Views, like Text or Shape, and won’t work if attached to another kind of View.

.fill is one that is implemented as a method on Shape, so it needs to be attached to an item of that type. That’s why it works on Rectangle, which is a Shape, but not if you put it after the .frame modifier, since that returns a type-erased View.

As for your LazyVStack/ForEach question, if you put a LazyVStack inside a ForEach then you are creating multiple LazyVStacks each filled with one or more elements, whereas if you place the ForEach inside the LazyVStack then you are creating one stack filled with multiple elements.

Thanks this really helps. As I learn more feels like I take a step back and see more equation on the chalkboard.