Swift 中的“相邻运算符位于非关联优先级组‘ComparisonPrecedence’”错误

Che*_*kie 4 logical-operators swift

在其他语言中,我已经完成了像这样的逻辑表达式,没有任何问题,但我在 Swift 中遇到了困难。

如果 appPurchased = false ANDenabled = true 并且按钮等于 photoLibraryBtn 或 takeVideoBtn,我希望此值评估为 true:

for button in buttonList {

    if appPurchased == false &&
        enabled == true &&
        button == photoLibraryBtn |
        button == takeVideoBtn {

        continue

    }

    button.isEnabled = enabled
    button.isUserInteractionEnabled = enabled
    button.alpha = alpha

}
Run Code Online (Sandbox Code Playgroud)

我收到错误“相邻运算符位于非关联优先级组‘ComparisonPrecedence’中”,我在 Google 上找不到任何结果。我在 Swift 中也没有看到像我这样的例子,所以我认为他们取消了单个“|” 管道字符,并且您只能使用双管道“||”,但要按一定顺序。但是,如果 appPurchased = false、enabled = true、button = photoLibraryBtn OR button = takeVideoBtn,我不希望 if 语句作为 true 传递。

rma*_*ddy 6

||不需要|||是“逻辑或”。|是“按位或”。

当您混合使用||和时&&,您需要括号以避免任何歧义。

根据您的描述,您想要:

if appPurchased == false &&
    enabled == true &&
    (button == photoLibraryBtn ||
    button == takeVideoBtn) {

    continue
}
Run Code Online (Sandbox Code Playgroud)

这也可以写成:

if !appPurchased &&
    enabled &&
    (button == photoLibraryBtn ||
    button == takeVideoBtn) {

    continue
}
Run Code Online (Sandbox Code Playgroud)