Default Keyword in Swift Parameter

Default keyword in Swift parameter

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.

Difficulties to assign default value to a parameter of a function

I don't think that is possible. The default value is inserted at the calling site, and therefore needs to be public, see also
Access control in swift 4.

A possible workaround would be to make the parameter optional,
and substitute nil by the default value locally:

class Foo {
private static let DefaultValue = 10

public func doTask(amount: Int? = nil) {
let amount = amount ?? Foo.DefaultValue
// ...
}
}

What is a default value parameter in Swift?

The default word here is just a placehoder of the documentation for the real default value.

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

could become

func NSLocalizedString(key: String, tableName: String? = "myDefaultTableName", bundle: NSBundle = NSBundle.mainBundle(), value: String = "myDefaultValue", #comment: String) -> String { ... }

Default optional parameter in Swift function

Optionals and default parameters are two different things.

An Optional is a variable that can be nil, that's it.

Default parameters use a default value when you omit that parameter, this default value is specified like this: func test(param: Int = 0)

If you specify a parameter that is an optional, you have to provide it, even if the value you want to pass is nil. If your function looks like this func test(param: Int?), you can't call it like this test(). Even though the parameter is optional, it doesn't have a default value.

You can also combine the two and have a parameter that takes an optional where nil is the default value, like this: func test(param: Int? = nil).

Parameter assigned with default (= default) means?

It doesn't mean anything. What you're seeing is due to a bug in the way the headers are translated into Swift. There is a default value here but you are not being shown what it is.

Another case in point in the headers is the recurring refrain where S.Generator.Element == S.Generator.Element. It's just an erroneous byproduct of the automatic translation process, and has been acknowledged as such by Apple.

What are the new for , at , in keywords in Swift3 function declarations?

The syntax allow you to label arguments twice - once for use within the function (the parameter name) and once for use when calling (the argument label). By default these two will be the same (like with sender in your example), but they differ when it makes a function more readable at the call site:

prepare(for: aSegue, sender: something)

being more readable-as-a-sentence than:

prepare(segue: aSegue, sender: something)

Which sounds like you're preparing the segue, not preparing for the segue.

for would be a dreadful name to use internally in the function to refer to the segue, so you can use a different one if you require.

The idea is to meet the sometimes conflicting goals of readability at the call site and sensible naming in the implementation.

When defining a function, if you are going to use separate argument labels and parameter names, you specify the argument label first, and the parameter name second:

func sayHello(to name: String) {
print("Hello " + name)
}

sayHello(to: "Pekka")

to only has relevance when calling the function. name is used inside the function.

How to make function that some parameters not required in when call it in iOS Swift 3?

You just have to make them nilable.

func myMethod(name name: String? = nil, age age: Int? = nil) {
print(name!)
}

Notice: when yo make parameters optional, you have to be careful about how to unwrap them. usually using if let syntax is helpful.

func myMethod(name name: String? = nil, age age: Int? = nil) {
if let name = name {
print(name)
}
}

you can also provide default value for them:

func myMethod(name name: String? = "Donald", age age: Int? = 10) {
print(name!)
}

Add default value to inout parameter using Swfit

The two keywords you're talking about, inout and var, are very different.

From Apple Documentation:

In-out parameters are passed as follows:

  1. When the function is called, the value of the argument is copied.
  2. In the body of the function, the copy is modified.
  3. When the function returns, the copy’s value is assigned to the original argument.

Therefore you can't give a default value to an inout parameter, as it would make the inout property completely useless.

What you can do is receive a normal (constant) parameter with a default value, and declare a new var with the same name this way (code from the Swift Evolution's Removing var from Function Parameters Proposal, with the addition of the default parameter):

func foo(i: Int = 5) {
var i = i
// now you can change i as you'd like
}


Related Topics



Leave a reply



Submit