WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 91: Flashzilla challenge #2

Forums > 100 Days of SwiftUI

I'm having real trouble with the second challenge in the Flashzilla project. I'm trying to work out how to tell from the removeCard function whether a card was answered correctly or incorrectly, and I started out trying to modify the closure so that I could call self.removal?(correctAnswer) after the DragGesture in CardView.swift. However, I've tried a number of ways to get this to work and I'm really struggling to do so – can anyone give me any hints as to how they've done this?

1      

I have a same problem here and I've been stuck on this for a while now :( : https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-91-flashzilla-challenge-2-3/7793

Hope you'll get some response cause I'd like to move forward and it's really frustrating :(

1      

I did eventually work this out! I went back to the Unwrap app and revised my closures, and came up with the below.

In ContentView.swift I inserted this code in place of the previous code:

let removal = { (correct: Bool) in
    print(correct)
    if self.removeWrongAnswers || correct {
        withAnimation {
            self.removeCard(at: index)
        }
    } else {
        // the DispatchQueue bit fixes the bug (source: HwS forums)
        let wrongCard = self.cards.remove(at: index)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.cards.insert(wrongCard, at: 0)
        }
    }
}

CardView(card: self.cards[index], removal: removal)
    .stacked(at: index, in: self.cards.count)
    .allowsHitTesting(index == self.cards.count - 1)
    .accessibility(hidden: index < self.cards.count - 1)

and then in CardView.swift I inserted this code in place of the existing .onEnded modifier:

.onEnded { _ in
    if abs(self.offset.width) > 100 {
        var correct: Bool

        if self.offset.width > 0 {
            self.feedback.notificationOccurred(.success)
            correct = true
        } else {
            self.feedback.notificationOccurred(.error)
            correct = false
        }

        self.removal?(correct)
    } else {
        self.offset = .zero
    }
}

3      

@johncoxon Thansk!! It's working for me now too :)

2      

OMG, I've just spent so long trying to figure this out.

I've then tried to implement your code but couldn't get that to work either. Then I noticed the comment about about the bug and the dispatch queue code.

Turns out my first attempt yesterday was working except for the bug... I've just added the dispatch queue code around my card insertion and boom, all working fine.

So frustrating, but also kind of a relief that I was so close to resolving this except for the bug. Incidentally, do you have a link to a discussion around the bug?

edit:

Don't worry, I've found it: https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-91-flashzilla-challenges-can-t-seem-to-readd-a-card-successfully/2037/6401

1      

Hello everyone! I'm also stucked on this challenge too. But I found better solution with less code and without DispatchQueue.main.

So Paul said that we should rethink our Card model and add an UUID(). When you create your List() it's identify your objects by UUID of the Card. And it's cause our problems. SwifUI manage object with the same UUID trying to remove from Array and append to it back almost at the same time .

So when you are trying to move Card to the end just set a new UUID() to the card. And everything will works fine without using delay on DispathchQueue.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.