void函数中的意外非void返回值

时间:2017-10-06 19:23:49

标签: swift closures

以下是我的代码。我正在学习闭包。我收到错误,我的函数不会返回值。有人可以帮忙吗?

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 个答案:

答案 0 :(得分:0)

使用此修改后的代码,因为您在函数中错过了返回类型( - > 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)