在Swift函数中默认可选参数

时间:2022-12-28 10:56:56

When I set firstThing to default nil this will work, without the default value of nil I get a error that there is a missing parameter when calling the function.

当我将firstThing设置为默认nil时,如果没有nil的默认值,我就会得到一个错误,即调用函数时缺少一个参数。

By typing Int? I thought it made it optional with a default value of nil, am I right? And if so, why doesn't it work without the = nil?

通过输入Int ?我认为它是可选的,默认值为nil,对吗?如果是的话,为什么没有= nil呢?

func test(firstThing: Int? = nil) {
    if firstThing != nil {
        print(firstThing!)
    }
    print("done")
}
test()

5 个解决方案

#1


73  

Optionals and default parameters are two different things.

选项和默认参数是两个不同的东西。

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

可选变量可以是nil,就是这样。

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

当省略该参数时,默认参数使用默认值,默认值如下所示:func测试(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.

如果指定的参数是可选的,则必须提供它,即使要传递的值是nil。如果函数看起来像这个func测试(param: Int?),则不能像这个test()那样调用它。即使参数是可选的,它也没有默认值。

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).

您还可以合并这两个参数,并使用一个可选参数,其中nil是默认值,比如:func test(param: Int?)= nil)。

#2


12  

The default argument allows you to call the function without passing an argument. If you don't pass the argument, then the default argument is supplied. So using your code, this...

默认参数允许您在不传递参数的情况下调用函数。如果不传递参数,则提供默认参数。使用你的代码。

test()

...is exactly the same as this:

…与此完全相同:

test(nil)

If you leave out the default argument like this...

如果你省略了这样的默认参数……

func test(firstThing: Int?) {
    if firstThing != nil {
        print(firstThing!)
    }
    print("done")
}

...then you can no longer do this...

…那么你就不能再这样做了……

test()

If you do, you will get the "missing argument" error that you described. You must pass an argument every time, even if that argument is just nil:

如果您这样做,您将得到您所描述的“丢失的参数”错误。你必须每次都传递一个参数,即使这个参数是nil:

test(nil)   // this works

#3


9  

Swift is not like languages like JavaScript, where you can call a function without passing the parameters and it will still be called. So to call a function in Swift, you need to assign a value to its parameters.

Swift不像JavaScript那样的语言,在这里您可以调用函数而不传递参数,它仍然会被调用。因此,要在Swift中调用函数,需要为其参数赋值。

Default values for parameters allow you to assign a value without specifying it when calling the function. That's why test() works when you specify a default value on test's declaration.

参数的默认值允许您在调用函数时指定一个值而不指定它。这就是为什么当您在test的声明中指定一个默认值时test()可以工作。

If you don't include that default value, you need to provide the value on the call: test(nil).

如果不包含该默认值,则需要提供调用时的值:test(nil)。

Also, and not directly related to this question, but probably worth to note, you are using the "C++" way of dealing with possibly null pointers, for dealing with possible nil optionals in Swift. The following code is safer (specially in multithreading software), and it allows you to avoid the forced unwrapping of the optional:

同样,与这个问题没有直接关系,但值得注意的是,您正在使用“c++”方法处理可能的空指针,以处理Swift中的可能的空选项。以下代码更安全(特别是在多线程软件中),它允许您避免强制展开可选代码:

func test(firstThing: Int? = nil) {
    if let firstThing = firstThing {
        print(firstThing)
    }
    print("done")
}
test()

#4


0  

"Optional parameter" means "type of this parameter is optional". It does not mean "This parameter is optional and, therefore, can be ignored when you call the function".

“可选参数”表示“该参数的类型是可选的”。它并不意味着“这个参数是可选的,因此在调用函数时可以忽略它”。

The term "optional parameter" appears to be confusing. To clarify, it's more accurate to say "optional type parameter" instead of "optional parameter" as the word "optional" here is only meant to describe the type of parameter value and nothing else.

术语“可选参数”似乎令人困惑。为了澄清这一点,更准确的说法是“可选类型参数”而不是“可选参数”,这里的“可选”一词只是用来描述参数值的类型,而不是其他。

#5


-1  

It is little tricky when you try to combine optional parameter and default value for that parameter. Like this,

当您试图将该参数的可选参数和默认值组合在一起时,这一点并不复杂。像这样,

func test(param: Int? = nil)

These two are completely opposite ideas. When you have an optional type parameter but you also provide default value to it, it is no more an optional type now since it has a default value. Even if the default is nil, swift simply removes the optional binding without checking what the default value is.

这两个观点完全相反。如果您有一个可选类型参数,但您也为它提供了默认值,那么它现在不再是一个可选类型,因为它有一个默认值。即使默认值是nil, swift也只是删除可选绑定,而不检查默认值是什么。

So it is always better not to use nil as default value.

所以最好不要用nil作为默认值。

#1


73  

Optionals and default parameters are two different things.

选项和默认参数是两个不同的东西。

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

可选变量可以是nil,就是这样。

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

当省略该参数时,默认参数使用默认值,默认值如下所示:func测试(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.

如果指定的参数是可选的,则必须提供它,即使要传递的值是nil。如果函数看起来像这个func测试(param: Int?),则不能像这个test()那样调用它。即使参数是可选的,它也没有默认值。

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).

您还可以合并这两个参数,并使用一个可选参数,其中nil是默认值,比如:func test(param: Int?)= nil)。

#2


12  

The default argument allows you to call the function without passing an argument. If you don't pass the argument, then the default argument is supplied. So using your code, this...

默认参数允许您在不传递参数的情况下调用函数。如果不传递参数,则提供默认参数。使用你的代码。

test()

...is exactly the same as this:

…与此完全相同:

test(nil)

If you leave out the default argument like this...

如果你省略了这样的默认参数……

func test(firstThing: Int?) {
    if firstThing != nil {
        print(firstThing!)
    }
    print("done")
}

...then you can no longer do this...

…那么你就不能再这样做了……

test()

If you do, you will get the "missing argument" error that you described. You must pass an argument every time, even if that argument is just nil:

如果您这样做,您将得到您所描述的“丢失的参数”错误。你必须每次都传递一个参数,即使这个参数是nil:

test(nil)   // this works

#3


9  

Swift is not like languages like JavaScript, where you can call a function without passing the parameters and it will still be called. So to call a function in Swift, you need to assign a value to its parameters.

Swift不像JavaScript那样的语言,在这里您可以调用函数而不传递参数,它仍然会被调用。因此,要在Swift中调用函数,需要为其参数赋值。

Default values for parameters allow you to assign a value without specifying it when calling the function. That's why test() works when you specify a default value on test's declaration.

参数的默认值允许您在调用函数时指定一个值而不指定它。这就是为什么当您在test的声明中指定一个默认值时test()可以工作。

If you don't include that default value, you need to provide the value on the call: test(nil).

如果不包含该默认值,则需要提供调用时的值:test(nil)。

Also, and not directly related to this question, but probably worth to note, you are using the "C++" way of dealing with possibly null pointers, for dealing with possible nil optionals in Swift. The following code is safer (specially in multithreading software), and it allows you to avoid the forced unwrapping of the optional:

同样,与这个问题没有直接关系,但值得注意的是,您正在使用“c++”方法处理可能的空指针,以处理Swift中的可能的空选项。以下代码更安全(特别是在多线程软件中),它允许您避免强制展开可选代码:

func test(firstThing: Int? = nil) {
    if let firstThing = firstThing {
        print(firstThing)
    }
    print("done")
}
test()

#4


0  

"Optional parameter" means "type of this parameter is optional". It does not mean "This parameter is optional and, therefore, can be ignored when you call the function".

“可选参数”表示“该参数的类型是可选的”。它并不意味着“这个参数是可选的,因此在调用函数时可以忽略它”。

The term "optional parameter" appears to be confusing. To clarify, it's more accurate to say "optional type parameter" instead of "optional parameter" as the word "optional" here is only meant to describe the type of parameter value and nothing else.

术语“可选参数”似乎令人困惑。为了澄清这一点,更准确的说法是“可选类型参数”而不是“可选参数”,这里的“可选”一词只是用来描述参数值的类型,而不是其他。

#5


-1  

It is little tricky when you try to combine optional parameter and default value for that parameter. Like this,

当您试图将该参数的可选参数和默认值组合在一起时,这一点并不复杂。像这样,

func test(param: Int? = nil)

These two are completely opposite ideas. When you have an optional type parameter but you also provide default value to it, it is no more an optional type now since it has a default value. Even if the default is nil, swift simply removes the optional binding without checking what the default value is.

这两个观点完全相反。如果您有一个可选类型参数,但您也为它提供了默认值,那么它现在不再是一个可选类型,因为它有一个默认值。即使默认值是nil, swift也只是删除可选绑定,而不检查默认值是什么。

So it is always better not to use nil as default value.

所以最好不要用nil作为默认值。