Swift: function with default parameter before non-default parameter

后端 未结 4 1067
忘了有多久
忘了有多久 2021-02-01 13:01

say I have a function that has non-default parameter after default parameter like this:

func f(first:Int = 100, second:Int){}

how can I call it

4条回答
  •  你的背包
    2021-02-01 14:02

    You should have the default parameters at the end of the parameter list.

    func f(second:Int, first:Int = 100){}
    f(10)
    

    Place parameters with default values at the end of a function’s parameter list. This ensures that all calls to the function use the same order for their non-default arguments, and makes it clear that the same function is being called in each case.

    Documentation link

提交回复
热议问题