BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

Question: " Generic struct 'ForEach' requires that 'finalChoice' conform to 'RandomAccessCollection' "

Forums > 100 Days of SwiftUI

@boat  

Hello,

I'm trying to make the ForEach work in the var body.

But at the ForEach line, Xcode tells me "Generic struct 'ForEach' requires that 'Choice' conform to 'RandomAccessCollection'"

Note that the finalChoice that @ObservedOject var usersChoice conforms to, is a Class conformesto ObservableObject with a sole property wrapped in @Published var finalchoice


struct SecondView: View {

    @ObservedObject var usersChoice: finalChoice

    var body: some View {

            VStack (spacing:50) {
                Spacer()
                Text ("Your Choice: ")
                ForEach (usersChoice, id: \.self) {
                    Text ("\($usersChoice)")
        }

                Button ("Thanks") {
                }
                ;Spacer()
    }

    }
}

struct SecondView_Previews: PreviewProvider {
    static var previews: some View {
        SecondView(usersChoice: finalChoice())
    }
}

What does this piece of warning mean ?

Generic struct 'ForEach' requires that 'finalChoice' conform to 'RandomAccessCollection' ?

I googled, and people's questions are all very specific on their own case. It seem there's no one broad explanation on it.

1      

I wrote a short article in the forums about ForEach.

See: For Each Notes

I think i answered this question there!

For each is a factory. It takes a COLLECTION of things (like an array of Warriors, or Tip Percentages) and MAKES a view for each item in that array. Perhaps, this is why your collection must conform to RandomAccessCollection protocol.

In your code usersChoice is a var of type finalChoice. It's not a collection of anything. XCode is telling you finalChoice is not a collection of things, yet you are giving it to ForEach to make views.

What are you trying to MAKE inside your ForEach statement?

1      

Remember the solution from December - Project 2 ForEach(number)

How have you defined 'finalChoice'? From the error you are getting is that it is not a RandomAccessCollection Also since you are using

ForEach (usersChoice, id: \.self) {
    Text ("\($usersChoice)")
}

the values in usersChoice have to have unique identifiers, otherwise you cannot use id: \.self, as there would be two or more duplicated values and the code would not know how to choose between them. Whatever you use of the id: in the ForEach loop has to have a unique value.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.