使用 Swift 将默认值添加到输入参数
Add default value to inout parameter using Swfit
在 Swift 2 中可以执行以下操作:
class SomeType {
static let singletonInstance = SomeType()
func someFunction(var mutableParameter: SomeType = SomeType.singletonInstance) {
...
}
}
但是在 Swift 3 中,函数参数的 var
关键字将被删除,取而代之的是 inout
。我无法使用 inout
关键字获得与上述相同的结果。
class SomeType {
static let singletonInstance = SomeType()
func someFunction(inout mutableParameter: SomeType = SomeType.singletonInstance) {
...
}
}
相反,我收到 "Default argument value of type 'SomeType' cannot be converted to type 'inout SomeType'"
错误
我的问题是是否可以使用默认值 inout
?
您所说的两个关键字 inout
和 var
非常不同。
In-out
parameters are passed as follows:
- When the function is called, the value of the argument is copied.
- In the body of the function, the copy is modified.
- When the function returns, the copy’s value is assigned to the original argument.
因此您不能为 inout
参数指定默认值,因为这会使 inout
属性 完全无用。
你可以做的是接收一个普通的(常量)参数和一个默认值,然后用这种方式声明一个新的同名的var
(代码来自Swift Evolution's Removing var from Function Parameters Proposal,加上默认参数):
func foo(i: Int = 5) {
var i = i
// now you can change i as you'd like
}
任何需要默认inout
参数的人可以考虑这样的解决方案:
class SomeType {
static let singletonInstance = SomeType()
func someFunction(inout mutableParameter: SomeType) {
...
}
// Declare function without 'mutableParameter':
func someFunction() {
someFunction(SomeType.singletonInstance)
}
}
当您指定参数的默认值时,Swift 编译器会自动为您生成一个不带参数的函数。在这个解决方案中,我们手动完成。
您可以创建第二个函数签名来“模拟”默认值
func foo() {
var v: Int = 0 // your default value
foo(bar: &v)
}
func foo(bar: inout Int) {
// do stuff
}
在 Swift 2 中可以执行以下操作:
class SomeType {
static let singletonInstance = SomeType()
func someFunction(var mutableParameter: SomeType = SomeType.singletonInstance) {
...
}
}
但是在 Swift 3 中,函数参数的 var
关键字将被删除,取而代之的是 inout
。我无法使用 inout
关键字获得与上述相同的结果。
class SomeType {
static let singletonInstance = SomeType()
func someFunction(inout mutableParameter: SomeType = SomeType.singletonInstance) {
...
}
}
相反,我收到 "Default argument value of type 'SomeType' cannot be converted to type 'inout SomeType'"
错误我的问题是是否可以使用默认值 inout
?
您所说的两个关键字 inout
和 var
非常不同。
In-out
parameters are passed as follows:
- When the function is called, the value of the argument is copied.
- In the body of the function, the copy is modified.
- When the function returns, the copy’s value is assigned to the original argument.
因此您不能为 inout
参数指定默认值,因为这会使 inout
属性 完全无用。
你可以做的是接收一个普通的(常量)参数和一个默认值,然后用这种方式声明一个新的同名的var
(代码来自Swift Evolution's Removing var from Function Parameters Proposal,加上默认参数):
func foo(i: Int = 5) {
var i = i
// now you can change i as you'd like
}
任何需要默认inout
参数的人可以考虑这样的解决方案:
class SomeType {
static let singletonInstance = SomeType()
func someFunction(inout mutableParameter: SomeType) {
...
}
// Declare function without 'mutableParameter':
func someFunction() {
someFunction(SomeType.singletonInstance)
}
}
当您指定参数的默认值时,Swift 编译器会自动为您生成一个不带参数的函数。在这个解决方案中,我们手动完成。
您可以创建第二个函数签名来“模拟”默认值
func foo() {
var v: Int = 0 // your default value
foo(bar: &v)
}
func foo(bar: inout Int) {
// do stuff
}