error: Execution was interrupted, reason: signal SIGABRT. In swift playgrounds

Viewed 1143

I'm getting this error when I try to run my playground in Xcode:

error: Execution was interrupted, reason: signal SIGABRT.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

This is my playground code:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let titleLabel = UILabel()
        titleLabel.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        titleLabel.text = "Title"
        titleLabel.textColor = .black

        let subtitleLabel = UILabel()
        subtitleLabel.text = "Subtitle"
        subtitleLabel.textColor = .gray
        subtitleLabel.frame = CGRect(x: 150, y: 200, width: 200, height: 20)

        NSLayoutConstraint.activate([
            titleLabel.bottomAnchor.constraint(equalToSystemSpacingBelow: subtitleLabel.topAnchor, multiplier: 10)
        ])

        view.addSubview(subtitleLabel)
        view.addSubview(titleLabel)
        self.view = view
    }
}
PlaygroundPage.current.liveView = MyViewController()

How do I fix this error? Is this a bug in Xcode? Or is there something wrong with my code?

1 Answers

Inside loadView method reorder the code. move up addSubview before adding the constraint

view.addSubview(subtitleLabel)
view.addSubview(titleLabel)

NSLayoutConstraint.activate([
            titleLabel.bottomAnchor.constraint(equalToSystemSpacingBelow: subtitleLabel.topAnchor, multiplier: 10)
        ])

self.view = view

The error will disappear.

Related