swift - 错误 : Execution was interrupted, 原因 : signal SIGABRT. 在 swift playgrounds

标签 swift xcode uiview swift-playground

当我尝试在 Xcode 中运行 playground 时出现此错误:

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.

这是我的 Playground 代码:

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()

如何修复此错误?这是 Xcode 中的错误吗?还是我的代码有问题?

最佳答案

loadView 方法中重新排序代码。在添加约束之前向上移动addSubview

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

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

self.view = view

错误将消失。

关于swift - 错误 : Execution was interrupted, 原因 : signal SIGABRT. 在 swift playgrounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61635669/

相关文章:

ios - 快速打开文件

iphone - 为什么 UIButton 标题不显示?

swift - 在 Swift 中用逗号分隔多个 if 条件

ios - 使用 Alamofire 使用参数数组上传多个文件

swift - 将 swift 中的对象层次结构映射到 firebase

ios - Swift - 如何在 MVC 中正确构建按钮?

objective-c - 如何以编程方式嵌入 Nib ?

SwiftUI 在变量更改时调用函数

xcode - Nib 的 IBOutlet 未连接(nil)

ios - 使用推送通知服务扩展上传应用程序