2

I am getting the following error "Closure containing control flow statement cannot be used with function builder 'ViewBuilder'" Not able to find similar troubleshoot anywhere.

struct FavoriteView: View {
    @EnvironmentObject var userData: UserData
    @State var isfavorite = false
    var favoriteindex = [1,2,3]

    var body: some View {
       NavigationView {
          List {
             ForEach(userData.labvaluesUserdata) {section in
                for numbers in favoriteindex {
                   if section.id == (numbers) {
                      ItemRow(list: section)
                   }
                }
            }
         }
      }
   }
}

With this I am able to get the first index. Any simple way to loop through ?

List {
   ForEach(userData.labvaluesUserdata) { section in
      if section.id == self.favoriteindex.first {
         ItemRow(list: section)
      }
   }
}
4
  • Did you see this: jasonzurita.com/swiftui-if-statement - it has exactly that error and a potential workaround. Commented Nov 27, 2019 at 16:00
  • Might have something to do with 7 LEFT braces, and only 5 Right braces ? Commented Nov 27, 2019 at 16:04
  • Yeah had seen that. But that is for a Bool value. For me, I need to loop over each integer in the array "favoriteindex" @koen Commented Nov 27, 2019 at 16:05
  • @donPablo thanks for pointing out that. But that was due to error in pasting the code here as I had to reduce some navigation bar code. I have edited the code now. Commented Nov 27, 2019 at 16:11

1 Answer 1

2

You can do it, and it's simple as that

enum SectionType: Identifiable {
    var id: Int {
        switch self {
        case .first:
            return 1
        case .second:
            return 2
        case .third:
            return 3
        }
    }

    case first
    case second
    case third
}

struct UserData {
    var labvaluesUserdata: [SectionType] = [.first, .second, .third, .first, .second]
}

struct ItemRow: View {
    var list: SectionType

    var body: some View {
        Text("\(list.id)")
    }
}

struct ContentView: View {
    @State var userData = UserData()
    @State var favoriteindex: [Int] = [2, 3]

    var body: some View {
        NavigationView {
            List {
                ForEach(userData.labvaluesUserdata) {section in
                    if self.favoriteindex.contains(where: { $0 == section.id }) {
                        ItemRow(list: section)
                    }
                }
            }
        }
    }
}

Updated: added brand new solution. Tried it and it works

Sign up to request clarification or add additional context in comments.

5 Comments

What is SectionType here ? I am getting this error "Use of undeclared type 'SectionType' "
oh, man It's type of your section in labvaluesUserdata array. So just replace it with your values type of labvaluesUserdata array.
Nope!!! labvaluesUserdata is an array of Struct. I get this error ii I use "MTLStruct Type" ...Cannot invoke initializer for type 'ForEach<_, _, _>' with an argument list of type '([Labvalueslist], @escaping (MTLStructType) -> some View)'
I was able to go further but held up here stackoverflow.com/questions/59268923/…
@Dar I've updated my answer. Check it. Also it solves stackoverflow.com/questions/59268923/… So you can close it if it's possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.