Group Group Group Group Group Group Group Group Group

Chapter 12 - don't understand most of the helper methods + DeckView

Hello, I really appreciate this book. It is very clear and helpful.

However I am really puzzled with the Learning features that we are building in Chap. 12.

  • LearningStore initializer - why are you using the so complicated getNextCard() and getLastCard() helper methods within the init to set the card parameter?

  • As we are incrementing the LearningStore score (which is @Published) when the CardView is dragged on the left, the LearnView view is rebuilt. Thus we call the getCardView(for card: FlashCard) for each remaining FlashCard. In this example there are 8 of them when we start. As we are never changing the LearningStore deck, there are always 8 FlashCard within this deck even after dragging CardView. So I don’t understand that we get the expected result (the stack of CardView is decreasing) after a left drag: the score is increased and therefore we rebuild LearnView and thus we are calling again the getCardView(for card: FlashCard) for every FlashCard in the deck (always 8). We should get again all 8 CardView on the ZStack.

  • I do not get the fact that we are using @ObservedObject private var deck: FlashDeck within DeckView. We are never modifying this deck. When I change the declaration to private var deck: FlashDeck, it just works fine

  • I do not understand the getCarView(for card: FlashCard) -> CardView helper function.
    private func getCardView(for card: FlashCard) -> CardView {
    let activeCards = deck.cards.filter { $0.isActive == true }
    if let lastCard = activeCards.last {
    if lastCard == card {
    return createCardView(for: card)
    }
    }

    let view = createCardView(for: card)

    return view
    }
    What is the point of finding the lastCard as we are anyway creating the view the same way if it is the last card or not. Just having the 2 last lines of code within this function just works fine.

  • FlashCard isActive property. What is the point of this property? We never change it, it is always true and we are testing it at least twice.

I thank you very much to explain to me the above.

Actually I found out the answer to my 2nd question. The trick is that the dragged cards disappear just because we are moving them away outside of the screen but they are not removed. OK for a small amount of FlashCards but I would imagine that we manage the deck in another way.

@tristanouin Please check out the updated version of the book when you get a chance:

https://store.raywenderlich.com/products/swiftui-by-tutorials

I hope it helps!