Why do I need underscores in swift?

后端 未结 4 631
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 16:51

Here it says, \"Note: the _ means “I don’t care about that value”\", but coming from JavaScript, I don\'t understand what that means.

The only way I can

4条回答
  •  感情败类
    2020-11-30 17:28

    func divmod(_ a: Int, _ b:Int) -> (Int, Int) {
        return (a / b, a % b)
    }
    
    func divmod(a: Int, b:Int) -> (Int, Int) {
        return (a / b, a % b)
    }
    

    The _ is a placeholder for the parameter name. In your example, you call them differently, in the second function, you need to write the parameter name a: 1.

    Swift's function name convention is funcName(param1:param2:), and it needs the _ as a placeholder to create the name of the function.

    In the first name, the name is

    divmod(_:_:)
    

    Whereas the second is

    divmod(a:b:)
    

提交回复
热议问题