Could not find an overload for '/' that accepts the supplied arguments

前端 未结 2 1274
北海茫月
北海茫月 2020-12-13 06:09
// Playground - noun: a place where people can play


func getAverage(numbers: Int...) -> Double{
    var total = 0
    var average:Double = 0

    for number in          


        
相关标签:
2条回答
  • 2020-12-13 07:01

    There are no such implicit conversions in Swift, so you'll have to explicitly convert that yourself:

    average = Double(total) / Double(numbers.count)
    

    From The Swift Programming Language: “Values are never implicitly converted to another type.” (Section: A Swift Tour)

    But you're now using Swift, not Objective-C, so try to think in a more functional oriented way. Your function can be written like this:

    func getAverage(numbers: Int...) -> Double {
        let total = numbers.reduce(0, combine: {$0 + $1})
        return Double(total) / Double(numbers.count)
    }
    

    reduce takes a first parameter as an initial value for an accumulator variable, then applies the combine function to the accumulator variable and each element in the array. Here, we pass an anonymous function that uses $0 and $1 to denote the first and second parameters it gets passed and adds them up.

    Even more concisely, you can write this: numbers.reduce(0, +).

    Note how type inference does a nice job of still finding out that total is an Int.

    0 讨论(0)
  • 2020-12-13 07:04

    The compiler picks an implementation of the / operator based on your input and output parameters. In your case the input parameters are two Int values and the output parameter is Double. This is what the compiler is looking for:

    func / (left: Int, right: Int) -> Double
    

    However, there is no such implementation of the / operator which is why you get the error. When you do Double(total/numbers.count), your output parameter changes to Int which is why the compiler picks the following implementation for the / operator which exists:

    func / (left: Int, right: Int) -> Int
    

    This is why you get 4instead of 4.5 as a result, even though you convert the result into a Double afterwards.

    Your can provide your own implementation of the / operator which first converts your numbers into Doubles:

    func / (left: Int, right: Int) -> Double {
        return Double(left) / Double(right)
    }
    

    Then you can do the following:

    let a: Int = 3
    let b: Int = 2
    let c: Double = a/b  // -> 1.5
    
    0 讨论(0)
提交回复
热议问题