One-line closure without return type

前端 未结 2 2039

In Swift, if a closure holds only a single statement, it automatically returns the value returned from that single statement.

This does not feel very natural in all

2条回答
  •  臣服心动
    2020-12-05 15:01

    The reason this happens is the shorthand for single line expression closures. There is an implicit 'return' in your closure as it is written.

    let closure: () -> () = {
        StringReturningFunc()
        return
    }
    

    Writing it like that should work

提交回复
热议问题