ios - Function Parameter Names don't behave according to documentation -
according manual:
function parameter names
these parameter names used within body of function itself, , cannot used when calling function. these kinds of parameter names known local parameter names, because available use within function’s body.
join("hello", "world", ", ") func join(s1: string, s2: string, joiner: string) -> string { return s1 + joiner + s2 }
but code doesn't compile:
error: missing argument labels 's2:joiner:' in call join("hello", "world", ", ") ^ s2: joiner:
only when try 1 parameter, becomes optional
join("hello") func join(s1: string) -> string { return s1 }
even more annoying, it's not permissible use first 1 @ all:
join(s1: "hello")
extraneous argument label 's1:' in call join(s1: "hello")
did miss while reading documentation covering functions?
the behavior different between functions , methods.
for method, default behavior use local name external name arguments after first.
you can think of methods defaulting to:
func method(_ arg1: anyobject?, #arg2: anyobject?, #arg3: anyobject?) { }
you can explicitly state how want handle arguments
func join(_ s1: string, _ s2: string, _ joiner: string) -> string { return s1 + joiner + s2 }
the swift programming language | language guide | methods
specifically, swift gives first parameter name in method local parameter name default, , gives second , subsequent parameter names both local , external parameter names default. convention matches typical naming , calling convention familiar writing objective-c methods, , makes expressive method calls without need qualify parameter names.
Comments
Post a Comment