"可选类型'(() -> Bool)?'不能用作布尔值;在委托函数调用中测试"!= nil"代替"



我的目标是返回其他类中函数的bool值。错误声称该功能是可选的,但我不明白它是如何可选的。我什至试图强制将其解开,但是它给了我错误"'(( -> bool'无法转换为'bool'。

我在类声明上方的ManageCaptureVC文件中具有我的协议:

protocol ManageCaptureVCDelegate: class {
    func selectedInterfaceOrientationIsLandscape() -> Bool
}

我在托管类中定义的委托:

weak var delegate: ManageCaptureVCDelegate?

和我的if语句尝试检查布尔值:

if self.delegate?.selectedInterfaceOrientationIsLandscape {
/*code*/
}

代表类中的原始功能是:

func selectedInterfaceOrientationIsLandscape() -> Bool {
    if(selectedInterfaceOrientation == interfaceOrientations.landscapeLeft ||
        selectedInterfaceOrientation == interfaceOrientations.landscapeRight){
        return true
    }
    return false
}

您需要调用该功能,而不是引用函数。

更改:

if self.delegate?.selectedInterfaceOrientationIsLandscape  {

to:

if self.delegate?.selectedInterfaceOrientationIsLandscape()  {

,但由于您使用的是可选的链接,因此应该是:

if self.delegate?.selectedInterfaceOrientationIsLandscape() ?? false {

最新更新