Swift Compiler认为,即使定义为非可选属性,属性也是可选的(UINavigationBar中的tintColor)

时间:2019-06-13 20:32:13

标签: swift uinavigationbar optional

    if let navigationBar = navigationController?.navigationBar {
        navigationBar.isTranslucent = true
        navigationBar.shadowImage = UIImage()
        navigationBar.tintColor = Color.grey1.uiColor
        navigationBar.setBackgroundImage(UIImage.createImage(withColor: UIColor.white.withAlphaComponent(0.92)), for: .default)
        navigationBar.titleTextAttributes = [
            .font: Font.monoBold.uiFont(ofSize: 17),
            .foregroundColor: navigationBar.tintColor
        ]
    }

编译器将navigationBar标记为非可选UINavigationBar。 这是有道理的,因为我们正在if let对其进行包装

UINavigationBar的界面中,它具有此属性tintColor

open var tintColor: UIColor!

所以navigationBar.tintColor似乎是非可选的吧?

但是,编译器决定在此处不同意:

enter image description here

  

从“ UIColor”隐式强制的表达?到“任何”

为什么编译器突然认为tintColor是可选的UIColor?

0 个答案:

没有答案