O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Fancy Parameters

Compared to Objective-C, Swift has a lot of extra options for what type of parameters can be passed in. Here are some examples.

Optional Parameter Types

In Swift, there is a new concept of optional types:

Optionals say either “there is a value, and it equals x” or “there isn’t a value at all.” Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes. Optionals are safer and more expressive than nil pointers in Objective-C and are at the heart of many of Swift’s most powerful features.

To indicate that a parameter type is optional (can be nil), just add a question mark after the type specification:

 func myFuncWithOptionalType(parameter: String?) {
  // function execution ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required