初始化代码:
MatchViewControllerPhone(nibName:"MatchViewControllerPhone", bundle: nil)
MatchViewControllerPhone类部分代码:
class MatchViewControllerPhone: UIViewController
@IBOutlet weak var container: UIScrollView!
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.container.delegate = self //在iOS8设备上报错:fatal error: unexpectedly found nil while unwrapping an Optional value
}
}
查看发现xib里的子控件都为nil导致的。
后来发现init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)中nibNameOrNil参数在iOS8中为空,所有有以下解决方案:
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
let classString = String(describing: type(of: self))
super.init(nibName: nibNameOrNil ?? classString, bundle: nibBundleOrNil)
}
ps:
1、在iOS9iOS10设备上是好的,估计被苹果修复了
2、在storyboard中也是好的,所以xib换成storyboard也是可以解决的。
本文介绍了在Swift项目中,针对iOS8设备使用`init(nibName:nibNameOrNil:bundle:nibBundleOrNil:)`初始化UIViewController时出现的`fatal error: unexpectedly found nil while unwrapping an Optional value`报错。问题源于nibNameOrNil参数在iOS8中可能为空。解决方案是重写init方法,使用自定义的nibName。在iOS9及更高版本和Storyboard中此问题已被修复,转换为Storyboard也可避免该问题。

783

被折叠的 条评论
为什么被折叠?



