Default arguments in Swift

Inspired by John Sundell I figured now would be as good a time as any to write something here.

Let’s kick it off with a nice Swift feature I saw on Twitter the other day where default arguments to functions are actual expressions, rather than constants. For example:

func someFunc(a: Int = 12 + 12) -> Int

While this example doesn’t make a lot of sense, a better use case would be together with some of Apples shared instance APIs.

func anotherFunc(fileManager: NSFileManager = NSFileManager.defaultManager())

This provides the convinience of not having to pass the file manager around everywhere but still makes the code more testable as you can pass in a test object from a test.