unexpected non-void return value in void function

筅森魡賤 提交于 2019-12-24 12:18:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!