xcode 二元运算符“/”不能应用于两个 (Int) 操作数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31132491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binary operator '/' cannot be applied to two (Int) operands
提问by user41805
I am getting a Binary operator '/' cannot be applied to two (Int) operandserror when I put the following code in a Swift playground in Xcode.
Binary operator '/' cannot be applied to two (Int) operands当我将以下代码放入 Xcode 的 Swift playground 时出现错误。
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.
上面是一个计算任何数字总和的函数。下面是一个计算数字平均值的函数。该函数正在sumOf()从自身内部调用该函数。
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 Ints, the same type and not different two different types.
注意:我在堆栈交换中到处寻找答案,但问题都与我的不同,因为我的问题涉及两个Ints,相同的类型而不是不同的两个不同类型。
I would like it if someone could help me to solve the problem which I have.
如果有人可以帮助我解决我遇到的问题,我会很高兴。
回答by vadian
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))
尽管有错误消息,但您似乎无法转发序列 (...) 运算符。函数sumOf(numbers)内的单个调用agv()会出错cannot invoke sumOf with an argument of type ((Int))
回答by Rohit Gupta
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
错误告诉你该怎么做。如果您参考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.
第二个论点必须是真实的。像这样转换它。我不使用 xcode,但我认为我的语法是正确的。
var avg:Float = ( sumOf(numbers) ) / Float( numbers.count )

