使用惰性var进行可选绑定的问题

时间:2018-07-31 02:27:52

标签: swift uipageviewcontroller lazy-initialization optional-binding

我正在使用UIPageViewController,并且有一个数组的ViewControllers

lazy var subviewControllers: [UIViewController] = {
    return[
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController1") as! ViewController1,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as! ViewController2,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
    ]
}()

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if let subview3 = subviewControllers[2] as? ViewController3 {
        subview3.customBar.alpha = 0.8 // <- error on this line
    }
}

但是有时在滚动时会出现此错误:

  

在展开可选值时意外发现nil

0 个答案:

没有答案