&引用;相邻运算符位于非关联优先组';比较优势'&引用;Swift中的错误

&引用;相邻运算符位于非关联优先组';比较优势'&引用;Swift中的错误,swift,logical-operators,Swift,Logical Operators,在其他语言中,我做过这样的逻辑表达式,没有任何问题,但我在Swift中遇到了困难 如果appPurchased=false,enabled=true,并且按钮等于photoLibraryBtn或takeVideoBtn,我希望该值为true: for button in buttonList { if appPurchased == false && enabled == true && button == photoLi

在其他语言中,我做过这样的逻辑表达式,没有任何问题,但我在Swift中遇到了困难

如果appPurchased=false,enabled=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

}

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

您需要的是
|
,而不是
<代码>|是“逻辑或”<代码>|是“按位或”

当你混合使用
|
&&
时,你需要括号来避免歧义

根据您的描述,您需要:

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

    continue
}
这也可以写成:

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

    continue
}