Ever wondered what the PlaygroundLiveView is all about.

Ok, just to be clear. I stumbled upon this kind of undefined behavior in today’s CS class while using the Xcode 9.3 beta 4.

The code

First, let’s take a look what code I tried to run:

//: Playground - noun: a place where people can play

import UIKit
import PlaygroundSupport

class BilligerTableViewController: UITableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! UITableViewCell
        cell.backgroundColor = UIColor.red
        return cell
    }
}

let viewController = BilligerTableViewController(style: .plain)
PlaygroundPage.current.liveView = viewController.tableView

At first glance, it looks like a basic implementation of a UITableViewController. After that we create an instance of the BilligerTableViewController and assign its tableView, which is indeed non-optional, to the current LiveView of the Playground.

Unfortunately the Playground prints this issue:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

It looks like something did not run as expected. The problem is that the cell we dequeue is nil and so for the force-cast fails and the program crashes.

You might expect that the PlaygroundLiveView just does not show any screen, but that’s not the case. It unexpectedly shows a cropped view of an iOS Simulator. Your playground is installed just like any other app and the playground’s name is equal to the app’s name you run while running your playground.

Digging deeper

By running this code, the simulator is forced to close the app and show the home screen instead. You can even swipe from the top to reach the notification center. It also looks like the PlaygroundSimulator does not use the iPhone X specific behaviors, such as swiping from the topper right corner to show the control center.

Back to the notification center; if you now swipe right you can reach the widgets, for example, the news widget. If you tap on any news on the news widget the simulator will open safari, but then almost immediately freezes.

The iOS Image

If you’re lucky running the playground so that the view looks like it is in portrait, then you can swipe down on the home screen and access spotlight search. You can now see that the iOS version is the iPad version as the keyboard has iPad specific keys. That makes sense because the Swift Playground App is only available for the iPad.

While viewing the Spotlight search you are able to see some content, e.g. calendar events or contacts associated with the simulator. But if you tap any of those Xcode will immediately freeze.

Conclusion

To put in a nutshell, the Swift Playground Simulator is just an iPad Simulator without a fixed screen size. It uses the same simulator image as the normal iPad simulator. Unfortunately, it is pretty limited in what you can do with it because any app not associated with you playground causes a freeze of both Xcode and the Simulator.

I hope you enjoyed reading this article. If you did you can follow me on twitter ;-).

One Response

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Back to Top