Swift 4 编程语言,输入参数不适用于函数类型作为参数
Swift 4 Programming Language, inout parameter is not working for FunctionType as Paramter
这是 swift 文档中的示例代码。我正在学习 swift 语言,我看到函数类型作为参数,示例代码没有 inout 关键字。但是我试图将它与 inout 参数一起使用,但是下面的示例没有按预期工作。
https://docs.swift.org/swift-book/LanguageGuide/Functions.html(函数类型为 Return 类型)
//Function Types as Return Types
func stepForward(_ input: inout Int) -> Int {
return input + 1
}
func stepBackward(_ input: inout Int) -> Int {
return input - 1
}
func chooseStepFunction(backward: Bool) -> (inout Int) -> Int {
let a = backward ? stepBackward : stepForward
return a
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
print(moveNearerToZero(¤tValue))
print(currentValue)
实际产量
2个
3
预期输出
2个
2
因为 CurrentValue 是 inout 参数。将 currentValue 作为 3 传递最初使用 stepBackward() 方法打印值 2
我想保持减量后的值。
但是这里不维护currentValue
那是因为在应用算术之后您实际上并没有为参数赋值,您只是返回新值而没有赋值。试试下面的代码
//Function Types as Return Types
func stepForward(_ input: inout Int) -> Int {
input += 1
return input
}
func stepBackward(_ input: inout Int) -> Int {
input -= 1
return input
}
func chooseStepFunction(backward: Bool) -> (inout Int) -> Int {
let a = backward ? stepBackward : stepForward
return a
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
print(moveNearerToZero(¤tValue))
print(currentValue)
你可以在没有任何东西的情况下尝试 return。只需在函数中传递当前值并更新值,它将自动更新当前值。
func stepForward(_ input: inout Int) {
input = input + 1
}
func stepBackward(_ input: inout Int) {
input = input - 1
}
func chooseStepFunction(backward: Bool, currentValue: inout Int) {
backward ? stepBackward(¤tValue) : stepForward(¤tValue)
}
var currentValue = 3
chooseStepFunction(backward: currentValue > 0, currentValue: ¤tValue)
print(currentValue)
您的问题是您从未更改过 currentValue 值!
您应该更改 chooseStepFuction 方法中的 currentValue 值!
chooseStepFunction(backward: currentValue > 0,
currentValue: ¤tValue)
这是 swift 文档中的示例代码。我正在学习 swift 语言,我看到函数类型作为参数,示例代码没有 inout 关键字。但是我试图将它与 inout 参数一起使用,但是下面的示例没有按预期工作。
https://docs.swift.org/swift-book/LanguageGuide/Functions.html(函数类型为 Return 类型)
//Function Types as Return Types
func stepForward(_ input: inout Int) -> Int {
return input + 1
}
func stepBackward(_ input: inout Int) -> Int {
return input - 1
}
func chooseStepFunction(backward: Bool) -> (inout Int) -> Int {
let a = backward ? stepBackward : stepForward
return a
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
print(moveNearerToZero(¤tValue))
print(currentValue)
实际产量 2个 3
预期输出 2个 2
因为 CurrentValue 是 inout 参数。将 currentValue 作为 3 传递最初使用 stepBackward() 方法打印值 2
我想保持减量后的值。
但是这里不维护currentValue
那是因为在应用算术之后您实际上并没有为参数赋值,您只是返回新值而没有赋值。试试下面的代码
//Function Types as Return Types
func stepForward(_ input: inout Int) -> Int {
input += 1
return input
}
func stepBackward(_ input: inout Int) -> Int {
input -= 1
return input
}
func chooseStepFunction(backward: Bool) -> (inout Int) -> Int {
let a = backward ? stepBackward : stepForward
return a
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
print(moveNearerToZero(¤tValue))
print(currentValue)
你可以在没有任何东西的情况下尝试 return。只需在函数中传递当前值并更新值,它将自动更新当前值。
func stepForward(_ input: inout Int) {
input = input + 1
}
func stepBackward(_ input: inout Int) {
input = input - 1
}
func chooseStepFunction(backward: Bool, currentValue: inout Int) {
backward ? stepBackward(¤tValue) : stepForward(¤tValue)
}
var currentValue = 3
chooseStepFunction(backward: currentValue > 0, currentValue: ¤tValue)
print(currentValue)
您的问题是您从未更改过 currentValue 值!
您应该更改 chooseStepFuction 方法中的 currentValue 值!
chooseStepFunction(backward: currentValue > 0,
currentValue: ¤tValue)