Swift make method parameter mutable?

后端 未结 7 842
滥情空心
滥情空心 2020-12-07 17:04

How can I deal with this error without creating additional variable?

func reduceToZero(x:Int) -> Int {
    while (x != 0) {
        x = x-1            //         


        
7条回答
  •  星月不相逢
    2020-12-07 17:52

    Solution using Swift5 with Functional Programming...

    func reduceToZeroFP(x:Int) -> Int {
        x == 0 ? x : reduceToZeroFP(x: x - 1)
    }
    

提交回复
热议问题