Binary operator "<=" can't be applied to operands of type Bool and Int

时间:2017-03-02 23:35:05

标签: swift swift3

if 1 <= A[i] <= 100 || 1 <= B[i] <= 100 

for the above line I get these two error. 1. Adjacent operators are in non-associative precedence group 'Comparision Precendence' 2. Binary operator "<=" can not be applied to type BOOL and Int.

2 个答案:

答案 0 :(得分:3)

try if (1 <= A[i] && A[i] <= 100) || (1 <= B[i] && B[i] <= 100)

答案 1 :(得分:3)

Joe的回答和Leo的评论都有效。我的偏好是Leo的方法(使用1...100 ~= A[i]),但无论你的船是什么漂浮。

那就是说,让我解释为什么你所做的是给你一个错误。没有任何括号来分解它,它会评估从左到右。因此,如果第一次检查&#34;是1&lt; = A [i]?&#34;,则会产生布尔答案。然后它试图询问&#34;是否为真&lt; = 100?&#34;,这没有任何意义。