Search code examples
swiftparametersfunction-parameter

swift with syntax on method parameters


I've seen this syntax in code examples and blogs, but have not seen it explained:

func exampleFunc(arg1: String, withOtherArgument otherArgument: String) -> Void {}

What does the with syntax do? Why is it necessary, and how is it different from just:

func exampleFunc(arg1: String, otherArgument: String) -> Void {}

Solution

  • As Apple’s documentation says:

    Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.

    In your first example withOtherArgument is the argument label and the otherArgument is the parameter name.