Swift Playground with Debugger Support

Swift Playground not showing errors

What you're seeing is correct behavior. If you want the "red marks" inline with your code, use a real project. Playgrounds don't show all errors, and if they do show them, they show them as you described, in console, first with the same message you would get in the "red marks" and then with the rather pointless crash log. (It is not Xcode that is crashing; it's the special playground execution process.)

So, this is what you see in a playground:

Sample Image

This is what you see an app project:

Sample Image

Swift Playground Crashes When Using 'view' in a UIViewController Class

Yes it’s a bug in Playgrounds 2.1 released on 30th April 2018. It was not an issue in the previous version.

For some reason the view does not exist in viewDidLoad even though it should. So it crashes because it’s nil.

Check out this article detailing the issue & some work around ideas.

https://makeapppie.com/2018/05/07/bug-workaround-swift-playgrounds-for-ipad/

Swift Playground error: Execution was interrupted, reason: signal SIGABRT

The problem is that you are adding the constraint to stackView rather than containerView.

The documentation for addConstraint states

The constraint to be added to the view. The constraint may only reference the view itself or its subviews.

containerView is the super view of stackView, not a sub view.

If you change your code to add the constraint to the containerView it will run

containerView.addConstraint(.init(item: stackView, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1, constant: 0))
containerView.addConstraint(.init(item: stackView, attribute: .leading, relatedBy: .equal, toItem: containerView, attribute: .leading, multiplier: 1, constant: 0))

You will probably want to add a trailing and a bottom constraint so that the stack view fills the whole container view. You will, of course, also need to add an arrangedSubview so that there is actually some content in the stack view.

It is generally simpler to add constraints by referencing layout guides rather than this older, more verbose, approach:

import UIKit
import PlaygroundSupport

let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
containerView.backgroundColor = UIColor.white

let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(stackView)

stackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true

let label = UILabel()
label.text = "Hello world"
label.textColor = .black
stackView.addArrangedSubview(label)
PlaygroundPage.current.liveView = containerView

Running GRDB in a playground

GRDB author here. To run those playgrounds:

  • Open the GRDB.xcworkspace workspace
  • Select the GRDBOSX scheme in the menu next to the Run/Stop buttons on the top left of the window
  • Select the Build command in the Product menu (Command-B)
  • Select a playground in the source list on the left of the window (they are in the "Playground" folder at the top)
  • Hit Run, and enjoy :-)


Related Topics



Leave a reply



Submit