遇到错误致命错误时:
Can’t unwrap Optional.None
追踪这一点并不容易.是什么导致这个错误?
码:
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 } }
解决方法
您收到此错误,因为您尝试访问没有值的可选变量.
例:
例:
// 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. }
您可以在官方Swift language guide中阅读有关选项的更多信息.