Is ForEach loop the only loop that's allowed inside a view?

I always get errors whenever I use the normal for loop and the repeat loop.
Answered by OOPer in 638956022

Is ForEach loop the only loop that's allowed inside a view?

As for now, YES.

To allow declarative syntax inside a view, view construction is made under the context of ViewBuilder.
The current implementation of ViewBuilder is based on @_functionBuilder, which is an Apple's original enhancement to Swift and does not permit for-in loop or repeat inside it.

It will be replaced by Swift Standard @resultBuilder in the near future. The new @resultBuilder would accept for-in loops in some specific cases, but it would not allow repeat inside.

SE-0289 Result builders

If you have some view construction logic in mind using for-in or repeat, you should better be accustomed to replace it using ForEach. No new feature would be added to @resultBuilder in the near future.
Accepted Answer

Is ForEach loop the only loop that's allowed inside a view?

As for now, YES.

To allow declarative syntax inside a view, view construction is made under the context of ViewBuilder.
The current implementation of ViewBuilder is based on @_functionBuilder, which is an Apple's original enhancement to Swift and does not permit for-in loop or repeat inside it.

It will be replaced by Swift Standard @resultBuilder in the near future. The new @resultBuilder would accept for-in loops in some specific cases, but it would not allow repeat inside.

SE-0289 Result builders

If you have some view construction logic in mind using for-in or repeat, you should better be accustomed to replace it using ForEach. No new feature would be added to @resultBuilder in the near future.
Thanks, OOPer. Do we know when this update will be released?

Do we know when this update will be released?

Hard to predict, as well as the behavior of SwiftUI.

https://forums.swift.org/t/se-0289-review-2-result-builders/40585/

Depends on how Evolution process of Swift would go.
I expect in a month or so, but it might be later.
Is ForEach loop the only loop that's allowed inside a view?
 
 
Q