首页 > 解决方案 > 在 UIViewController 类中使用“视图”时,Swift Playground 崩溃

问题描述

我在 Swift Playgrounds 应用程序中有以下代码,但是当我运行它时,我收到错误“Problem Running Playground”。我一步一步地运行代码,它似乎在view.backgroundColor = UIColor.red. 但是,这可以在 Xcode Playground 上完美执行。我的代码有问题还是这是 Swift Playgrounds 中的错误?

import UIKit
import PlaygroundSupport

class Test: UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.red
    }
}

PlaygroundPage.current.liveView = Test()

标签: swiftuiviewswift-playground

解决方案


是的,这是 2018 年 4 月 30 日发布的 Playgrounds 2.1 中的一个错误。在之前的版本中这不是问题。

由于某种原因,该视图在 viewDidLoad 中不存在,即使它应该存在。所以它崩溃了,因为它是零。

查看这篇文章,详细介绍问题和一些解决方法。

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


推荐阅读