【问题标题】:Swift Playground Crashes When Using 'view' in a UIViewController Class在 UIViewController 类中使用“视图”时,Swift Playground 崩溃
【发布时间】:2018-06-01 22:50:58
【问题描述】:

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

【问题讨论】:

  • If语句中换行,看看视图是否不是nil
  • 我包装它并逐步运行代码,显示视图!= nil,然后在尝试设置视图的背景颜色时崩溃。
  • 如果代码在 Xcode 上运行并且只在 Swift Playground 上崩溃,那么您的代码就可以使用。正如你所说,操场上最可能的错误。
  • 试试self.view.backgroundColor = UIColor.red
  • 同样的错误。一定是最近 Swift Playgrounds 更新的错误。

标签: swift uiview swift-playground


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多