Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UI tutorial ERROR "Closure containing control flow statement cannot be used with function builder 'ViewBuilder'"

I tried a SwiftUI tutorial, "Handling User Input".

https://developer.apple.com/tutorials/swiftui/handling-user-input

I tried implementing it with for instead of ForEach. But an error arose: "Closure containing control flow statement cannot be used with function builder 'ViewBuilder'".

FROM:

import SwiftUI

struct LandmarkList: View {
    @State var showFavoritesOnly = true
    
    var body: some View {
        NavigationView{
            List{
                Toggle(isOn: $showFavoritesOnly){
                    Text("Show FavatiteOnly")
                }
                
                ForEach(landmarkData) { landmark in
                    if !self.showFavoritesOnly || landmark.isFavorite {
                            NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                                LandmarkRow(landmark: landmark)
                            }
                        }
                }
            }
            .navigationBarTitle(Text("Landmarks"))
        }
    }
}

TO (I wrote):

import SwiftUI

struct LandmarkList: View {
    @State var showFavoritesOnly = true
    
    var body: some View {
        NavigationView{
            List{
                Toggle(isOn: $showFavoritesOnly){
                    Text("Show FavatiteOnly")
                }
                
                for landmark in landmarkData {
                    if $showFavoritesOnly || landmark.isFavorite {
                        NavigationLink(destination: LandmarkDetail(landmark: landmark)){
                            LandmarkRow(landmark: landmark)}
                        }
                }
            }
            .navigationBarTitle(Text("Landmarks"))
        }
    }
}
like image 853
msoniku Avatar asked Jun 28 '20 02:06

msoniku


2 Answers

The ForEach confirms to View, so at its core, it is a View just like a TextField. ForEach Relationships

You can't use a normal for-in because the ViewBuilder doesn't understand what is an imperative for-loop. The ViewBuilder can understand other control flow like if, if-else or if let using buildEither(first:), buildEither(second:), and buildif(_:) respectively.

like image 104
Hamza Jadid Avatar answered Oct 10 '22 17:10

Hamza Jadid


I was getting this error because of a missing associated value of an enum passed to one of my views.

Here's how it looked like before and after:

Before

Group {
    if let value = valueOrNil {
        FooView(
            bar: [
                .baz(arg1: 0, arg2: 3)
            ]
        )
    }
}

After

Group {
    if let value = valueOrNil {
        FooView(
            bar: [
                .baz(arg1: 0, arg2: 3, arg3: 6)
            ]
        )
    }
}
like image 2
Tamás Sengel Avatar answered Oct 10 '22 16:10

Tamás Sengel