56

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
1

1 Answer 1

91

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.

8
  • Ah, okey. That's why I was not able to find anything on the default keyword. Thank you.
    – LuckyLuke
    Commented Jul 28, 2014 at 9:59
  • 4
    Thanks! I googled for this but landed just on non-informtive Apple pages. But when typing my question here on SO I got this useful answer immediately :-)
    – qwerty_so
    Commented Jan 15, 2015 at 10:34
  • 1
    But why are we able to call it with all parameters then?
    – Warpzit
    Commented Jun 10, 2016 at 12:54
  • @Warpzit What do you mean by that? We can call it with all parameters because every default value can be overriden.
    – Sulthan
    Commented Jun 10, 2016 at 12:55
  • @Sulthan I read the first line: "This is not a valid Swift code, it's generated on the fly." but didn't ponder enough on the second line, but your comment was just the last thing I needed to understand it properly so thanks a lot.
    – Warpzit
    Commented Jun 10, 2016 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.