Default keyword in Swift parameter

IosSwift

Ios Problem Overview


When reading the initializer for a NSLocalizedString I see that some of the parameters are defaulted to a value default. What does the default keyword represent?

func NSLocalizedString(key: String, tableName: String? = default, bundle: NSBundle = default, value: String = default, #comment: String) -> String

Ios Solutions


Solution 1 - Ios

This is not a valid Swift code, it's generated on the fly.

The default here means that there is some default value but the generator cannot visualize it right for you to see it. The default value is technically an inlined function, therefore it cannot be easily converted to a simple declaration.

You can see similar declarations for assert

func assert(condition: @auto_closure () -> Bool,
            _ message: StaticString = default,
                 file: StaticString = default,
                 line: UWord = default)

Where file defaults to #file (__FILE__ in Swift 1.x) and line defaults to #line (__LINE__ in Swift 1.x).

In the case of NSLocalizedString, the default value is "Localizable", referencing the default localization file Localizable.strings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuckyLukeView Question on Stackoverflow
Solution 1 - IosSulthanView Answer on Stackoverflow