Using Completion Handlers in Xcode Playgrounds

Andrew Sowers
May 5, 2016 · 1 min read

When starting to learn Swift, you will eventually want to explore creating Swift closures as asynchronous completion handlers. Naturally, you’ll try and fire up a Playground and wire up a function that takes another function as an argument. But when you want to test your completion execution, you might have noticed that the debug console printed a mysterious () type instead of what you expected. What Xcode Playground is doing is saying that there was a function that executed, but the current page has completed its execution.

If you would like to tell the Xcode Playground to continue executing your code (including the code in the completion handler) you need t0 access the currentPage of the XCPlayground.XCPlaygroundPage and tell it to do so. This is done by executing XCPlayground.XCPlaygroundPage.currentPage.finishExecution() after importing XCPlayground at the top of your Xcode Playground code. I wrote up this Gist as an example:

Originally published at Haiku Robot.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store