Coding with Closures

To try out closures, we are going to call addPeriodicTimeObserver(forInterval:queue: using:), passing in a closure to call repeatedly when our podcast is playing. There’s a little housekeeping we have to do for this approach: the docs say that the return value is an object of type Any that we will eventually provide to removeTimeObserver to stop our updating. So, with the other properties near the top of ViewController.swift, add a property where we can hold on to this object. It’ll need to be an optional, since we won’t actually create it until long after init is done.

 private​ ​var​ playerPeriodicObserver : ​Any​?

We already cleaned up the player

Get iOS 10 SDK Development, 1st Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.