xcode - 二元运算符 '/' 不能应用于两个 (Int) 操作数

标签 xcode swift swift-playground

<分区>

当我将以下代码放入 Xcode 的 Swift playground 中时,出现Binary operator '/' cannot be applied to two (Int) operands 错误。

func sumOf(numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf()
sumOf(42, 597, 12)

上面是一个计算任意数字总和的函数。 下面是一个计算数字平均值的函数。该函数从自身内部调用 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);

注意:我在堆栈交换中到处寻找答案,但问题都与我的不同,因为我的涉及两个 Int,相同类型而不是不同的两个不同类型。

如果有人能帮助我解决我遇到的问题,我会很高兴。

最佳答案

尽管有错误消息,但您似乎无法转发序列 (...) 运算符。在 agv() 函数中单次调用 sumOf(numbers) 会出现错误 cannot invoke sumOf with an argument of type ((Int))

关于xcode - 二元运算符 '/' 不能应用于两个 (Int) 操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31132491/

相关文章:

iphone - 发射了多少颗子弹?

iphone - 当 UITextField 是第一响应者时,仍然可以选择 UIButton?

ios - 无法在 Playground 中加载 UITableView

swift - UIButtonType 未在 Swift Playground 中显示

objective-c - 使用 objc_getAssociatedObject() 时奇怪的 Swift 行为

Xcode 7 - 如何删除 ld : warning: -read_only_relocs cannot be used with x86_64

objective-c - 查找数组中重复次数最多的对象

ios - 在 Swift 中单击 CAShapeLayer 时检测触摸

ios - Xcode 7 beta 2 和 swift 相处不来

swift - tvOS:无论如何要在 AVPlayer 之外显示字幕吗?