本文介绍了无法打开'Optional.None'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到错误致命错误时:

跟踪这个并不容易。导致此错误的原因是什么?

It is not that easy to trace this. What causes this error?

import UIKit

class WelcomeViewController: UIViewController {
    let cornerRad:CGFloat = 10.0
    @IBOutlet var label:UILabel
    @IBOutlet var lvl1:UIButton
    @IBOutlet var lvl2:UIButton
    @IBOutlet var lvl3:UIButton
    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        lvl1.layer.cornerRadius = cornerRad
        lvl2.layer.cornerRadius = cornerRad
        lvl3.layer.cornerRadius = cornerRad
    }
}


推荐答案

您收到此错误,因为您尝试访问没有值的可选变量。
示例:

You get this error, because you try to access a optional variable, which has no value.Example:

// This String is an optional, which is created as nil.
var optional: String?

var myString = optional! // Error. Optional.None

optional = "Value"
if optional {
    var myString = optional! // Safe to unwrap.
}

您可以阅读更多关于期权的信息在官方。

You can read more about optionals in the official Swift language guide.

这篇关于无法打开'Optional.None'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:18