NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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

Forums > 100 Days of SwiftUI


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) {
                Text ("Your Choice: ")
                ForEach (usersChoice, id: \.self) {
                    Text ("\($usersChoice)")

                Button ("Thanks") {


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.


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?


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.


Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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.