Group Group Group Group Group Group Group Group Group

Your First iOS App - Part 33: Closures | Ray Wenderlich Videos

#1

Learn how to use an important construct in Swift called closures, which you often use to provide a block of code that is executed some time in the future.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5993-your-first-ios-app/lessons/33
#3

Dear Ray,
Thanks for this delightful course.
Coming from a JavaScript background, I was trying to go through your swift course while separating functions as much as possible.

In this episode you showed

handler: {
  action in
  self.start
})

But how would I best implement this if I had extracted the showAlert logic like so:

   func showAlert (_ dif: Int, _ points: Int) {
        let message = "The value is now \(currentValue)" +
            "\nyour target: \(targetValue)" +
            "\ndifference: \(dif)" +
            "\npoints: \(points)!!"
        let alert = UIAlertController(title: "Hello kidz!", message: message, preferredStyle: .alert)
        let action = UIAlertAction(title: "awesomeee", style: .default, handler: {
            action in
            self.startNewRound() // prevent this call to startNewRound, see below
        })
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
    }
    @IBAction func finishRound () {
        let dif = calculateDifference(targetValue, currentValue)
        let points = calculatePoints(dif)
        score += points
        showAlert(dif, points)
        startNewRound() // call startNewRound here only!
    }
#4

I have found there is a bug here in this tutorial. For some reason when I build and run, I hit OK and the app freezes and the only way to move to the next round is to click on one of the button. In the video though it shows that it moves on to the next round right after hitting OK? What could I possibly be doing wrong?

1 Like
#5

I’m also experiencing this issue in Swift 10.1

#6

Following along with some basic troubleshoots… it looks to me like the handler works properly. It calls self.startNewRound properly, which in turn calls updateLabels properly… But its there that it hangs until another button on the screen is pushed… Not sure why…

Could this be a bug in the simulator?

#7

I’m having the exact same problem… it will execute startNewRound if I put it somewhere else, but if I put it as an action like in the video then the app hangs and won’t execute the startNewRound

#8

Same issue for me too.

#9

Hey, great tutorial. My only question is why did You decide to put this code inside the action handler not the alert completion block?

#10

Great job…
The closure seems to get executed but does not update the screen. Apparently, it needs to run on the main UI thread. I tried using DispatchQueue, but was not successful. Will post update if I figure it out.

#11

@solopilot Do you still have issues with this?

#12

Thanks for following up.
I upgraded to Mojave & XCode 10.2.1 and it works !!
Previously, I had an earlier version of XCode and the Closure did not update the screen as expected. I wonder if I was imagining things !!

regards

#13

Same problem here. @shogunkaramazov, could you investigate why this is happening?