代码之家  ›  专栏  ›  技术社区  ›  Chewie The Chorkie

swift中的“相邻运算符位于非关联优先组”comparisonprecedence“错误

  •  0
  • Chewie The Chorkie  · 技术社区  · 7 年前

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

    如果apppurched=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
    
    }
    

    我得到错误“相邻的操作符在非关联优先组‘comparisonprecedence’中”,在Google上找不到任何结果。我也没有用swift看到类似我的例子,所以我认为他们去掉了单管“”字符,而你只应该使用双管“”字符,但按一定的顺序。但是,如果apppurched=false、enabled=true、button=photolibrarybtn或button=takevideobtn,我不希望if语句作为true传递。

    1 回复  |  直到 7 年前
        1
  •  2
  •   rmaddy    7 年前

    你需要 || 不是 | . γ 是“逻辑或”。 γ 是“位或”。

    当你混合的时候 γ && ,您需要括号以避免任何歧义。

    根据您的描述,您需要:

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

    这也可以写成:

    if !appPurchased &&
        enabled &&
        (button == photoLibraryBtn ||
        button == takeVideoBtn) {
    
        continue
    }
    
    推荐文章