@jimbo and @steer are stuck
@steer: I'm getting a weird runtime error
@Jimbo: Did you ever figure out what the problem was?
@twoStraws provides a subtle hint:
For a harder challenge: when the users gets an answer wrong, add that card
back into the array so the user can try it again.
HINT: Doing this successfully means rethinking the ForEach loop,
HINT: because relying on simple integers isn’t enough –
HINT: your cards need to be uniquely identifiable.
This challenge exposes you to a common problem when generating views via a ForEach
factory.
I like to think of ForEach
as a view factory!
See -> View Factory
or See -> View Factory
or See -> View Factory
Ask yourself: What is my ForEach
view factory trying to make?
This is a simple example, the ForEach just generates five text views.
ForEach(0..<deckOfCards.count-1) { index in
// The objects are identified by their PLACE in the array.
Text("Object # \(deckOfCards[index].name)") // View factory generates 5 Text views.
}
Notice in this example the objects are identified by their PLACE in the array. If you delete the second card (index = 1) the deckOfCards will have one less card, but will still have a card at index 1. The array re-numbers its contents.
But when you use ForEach to generate views containing objects, it's imporant for SwiftUI to have the ability to identify WHICH object is tapped, or deleted.
ForEach( deckOfCards ) { card in
// Advanced Topic.
// User may want to select or delete this card.
// SwiftUI needs to know which card!
PlayingCardView( for: card ) // <- BUILD a view for each card in the deck
}
Back of the Envelope Idea
If you select the Deuce of Clubs and remove it from the array named deckOfCards, how does SwiftUI know WHICH card to delete? If the card struct
conforms to the identifiable
protocol, each card will have a unique id
property.
Consider how you might >save< this card to a holding area (another array named tryAgainDeck
?). After the player goes through all the cards in the quiz, the game will cycle through the tryAgainDeck
, and add those cards back into the deckOfCards
array.
These are some random ideas. Neither of you showed code, so it's difficult to know how you approached this challenge.
Keep Coding