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:
This is what you see an app project:
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
Programmatically Place Partial Image Over Another in UIview Using Swift 3
Notification in Swift Returning Userinfo in Dictionary
Tableview Image Content Selection Color
Parametrized Unit Tests in Swift
"Ambiguous Reference to Member Map" When Attempting to Append/Replace Array Element
Swift Override Static Method Compile Error
"This Class Is Not Key Value Coding-Compliant" Using Coreimage
Access Class Property from Instance
Does Cocoa Connection Binding to Nstoolbaritem Prevent Deinitializing
Why Is Swift Counting This Grapheme Cluster as Two Characters Instead of One
Why Do I Get Source Kit Service Terminated Error
Enum Named 'Type' in Nested Class Fails
Swift Switch Case Compiler Error
Why Is My Libraries Not Able to Expand on The Cocoapods and Shows as Objective-C Not Swift