shippo7 shippo7 - 22 days ago 6
Swift Question

Declaring a function input parameter is equal to nil in Swift

func taskWithMethod(URLString: String, parameters: [String: AnyObject]?, queryParameters: [String: AnyObject]? = nil){}


What is the difference between
parameters
and
queryParameters
in this function? Looks like
queryParameters
is being defined to nil, but I can still pass the
queryParameters
value to this function.

Answer

That's a Default Parameter Value (see section "Default Parameter Values"). If no value is passed in, it defaults to nil.

For example, this function can be called like so:

taskWithMethod(URLString: someString, parameters: dict1, queryParameters: dict2)

but it can also be called like so:

taskWithMethod(URLString: someString, parameters: dict1)

in which case queryParameters is set to its default value, nil.