This is the sample code from swift documentation. I am learning swift language, and I saw the functiontype as parameter, The sample code does not have inout keyword. But I am trying to use this with inout paramter, but the below sample is not working as expected.
这是swift文档中的示例代码。我正在学习快速的语言,我看到函数类型作为参数,示例代码没有inout关键字。但我试图使用它与inout参数,但下面的示例没有按预期工作。
https://docs.swift.org/swift-book/LanguageGuide/Functions.html (Function Types as Return Types)
https://docs.swift.org/swift-book/LanguageGuide/Functions.html(函数类型作为返回类型)
//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)
Actual output 2 3
实际输出2 3
Expected output 2 2
预期产出2 2
Because CurrentValue is inout paramter. Passing the currentValue as 3 initially prints value 2 using stepBackward() method
因为CurrentValue不是很重要。将currentValue作为3传递最初使用stepBackward()方法打印值2
and I want to maintain the value after the decrement.
我想在减量后保持价值。
But the currentValue is not maintained here.
但是这里没有维护currentValue。
3 个解决方案
#1
3
That's because you are not actually assigning value to parameter after applying arithmetics you are just returning new value without assigning it. Try the following code
这是因为在应用算术之后你实际上并没有为参数赋值,而是只返回新值而不指定它。请尝试以下代码
//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)
#2
0
You can try it without return anything. Just pass current value in function and update the value it will automatically update current value.
你可以尝试它而不返回任何东西。只需在函数中传递当前值并更新该值,它将自动更新当前值。
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)
#3
0
your problem is that you never changed the currentValue value !
你的问题是你从未改变currentValue值!
you should change the currentValue value in chooseStepFuction method !
你应该在chooseStepFuction方法中改变currentValue值!
chooseStepFunction(backward: currentValue > 0,
currentValue: ¤tValue)
#1
3
That's because you are not actually assigning value to parameter after applying arithmetics you are just returning new value without assigning it. Try the following code
这是因为在应用算术之后你实际上并没有为参数赋值,而是只返回新值而不指定它。请尝试以下代码
//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)
#2
0
You can try it without return anything. Just pass current value in function and update the value it will automatically update current value.
你可以尝试它而不返回任何东西。只需在函数中传递当前值并更新该值,它将自动更新当前值。
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)
#3
0
your problem is that you never changed the currentValue value !
你的问题是你从未改变currentValue值!
you should change the currentValue value in chooseStepFuction method !
你应该在chooseStepFuction方法中改变currentValue值!
chooseStepFunction(backward: currentValue > 0,
currentValue: ¤tValue)