UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

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?

2      

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.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.