How to use Swift @autoclosure

后端 未结 6 1391
天命终不由人
天命终不由人 2020-12-02 04:44

I noticed when writing an assert in Swift that the first value is typed as

@autoclosure() -> Bool

with an overloaded method

6条回答
  •  青春惊慌失措
    2020-12-02 04:50

    It's just a way to get rid of the curly braces in a closure call, simple example:

        let nonAutoClosure = { (arg1: () -> Bool) -> Void in }
        let non = nonAutoClosure( { 2 > 1} )
    
        let autoClosure = { (arg1: @autoclosure () -> Bool) -> Void in }
        var auto = autoClosure( 2 > 1 ) // notice curly braces omitted
    

提交回复
热议问题