问题
Below is my code. I'm learning closures. I'm getting an error that my function would not return a value. Can someone help?
func operationOnNumbers(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) {
let result = operation(a, b)
print(result)
return result
}
let addClosure = {(a: Int, b: Int) in
a + b
}
operationOnNumbers(5, 7, operation: addClosure)
回答1:
Use this modified code as you have missed return type in the function ( -> Int)
func operationOnNumbers(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) -> Int{
let result = operation(a, b)
print(result)
return result
}
let addClosure = {(a: Int, b: Int) in
a + b
}
operationOnNumbers(5, 7, operation: addClosure)
来源:https://stackoverflow.com/questions/46612620/unexpected-non-void-return-value-in-void-function