问题
I am getting a Binary operator '/' cannot be applied to two (Int) operands
error when I put the following code in a Swift playground in Xcode.
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf()
sumOf(42, 597, 12)
The above was a function calculating the total sum of any numbers.
Below is a function calculating the average of the numbers. The function is calling the sumOf()
function from within itself.
func avg(numbers: Int...) -> Float {
var avg:Float = ( sumOf(numbers) ) / ( numbers.count ) //Binary operator '/' cannot be applied to two (Int) operands
return avg
}
avg(1, 2, 3);
Note: I have looked everywhere in stack exchange for the answer, but the questions all are different from mine because mine is involving two Int
s, the same type and not different two different types.
I would like it if someone could help me to solve the problem which I have.
回答1:
Despite the error message it seems that you cannot forward the sequence (...) operator. A single call of sumOf(numbers)
within the agv()
function gives an error cannot invoke sumOf with an argument of type ((Int))
回答2:
The error is telling you what to do. If you refer to https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html
/ Division.
A binary arithmetic operator that divides the number to its left by the number to its right.
Class of operands: integer, real
Class of result: real
The second argument has to be real. Convert it like so. I don't use xcode, but I think my syntax is correct.
var avg:Float = ( sumOf(numbers) ) / Float( numbers.count )
来源:https://stackoverflow.com/questions/31132491/binary-operator-cannot-be-applied-to-two-int-operands