Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default keyword in Swift parameter

Tags:

ios

swift

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
like image 390
LuckyLuke Avatar asked Jul 28 '14 09:07

LuckyLuke


People also ask

What does default mean in Swift?

Swift provides a default initializer for any structure or class that provides default values for all of its properties and doesn't provide at least one initializer itself. The default initializer simply creates a new instance with all of its properties set to their default values.

Is it possible to give a default value to a function parameter Swift?

Parameters can provide default values to simplify function calls and can be passed as in-out parameters, which modify a passed variable once the function has completed its execution. Every function in Swift has a type, consisting of the function's parameter types and return type.

What should you use to provide a default value for a variable in Swift?

However there is another data type in Swift called Optional, whose default value is a null value ( nil ). You can use optional when you want a variable or constant contain no value in it. An optional type may contain a value or absent a value (a null value).

What is Inout in Swift?

Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.


1 Answers

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.

like image 152
Sulthan Avatar answered Oct 06 '22 12:10

Sulthan