Optional Default Parameter Swift

[Solved] Optional Default Parameter Swift | Swift - Code Explorer | yomemimo.com
Question : Optional & Default Parameter Swift

Answered by : nayan-dave

Optionals and default parameters are two different things.
An Optional is a variable that can be nil, that's it.
Default parameters use a default value when you omit that parameter, this default value is specified like this: func test(param: Int = 0)
If you specify a parameter that is an optional, you have to provide it, even if the value you want to pass is nil. If your function looks like this func test(param: Int?), you can't call it like this test(). Even though the parameter is optional, it doesn't have a default value.
You can also combine the two and have a parameter that takes an optional where nil is the default value, like this: func test(param: Int? = nil).

Source : https://stackoverflow.com/a/37306047/12456741 | Last Update : Sat, 16 Jul 22

Answers related to optional default parameter swift

Code Explorer Popular Question For Swift