Swift optional chaining doesn't work in closure

后端 未结 4 1954
孤独总比滥情好
孤独总比滥情好 2020-12-03 13:45

My code looks like this. My class has an optional var

var currentBottle : BottleLayer?

BottleLayer has a method jiggle().

4条回答
  •  甜味超标
    2020-12-03 14:10

    let closure = { () -> () in
        self.currentBottle?.jiggle()
        return
    }
    

    Otherwise the compiler thinks the result of that statement should be returned from the closure and it realizes there is a mismatch between () (return type) and the optional returned by the statement (Optional). By adding an explicit return, the compiler will know that we don't want to return anything.

    The error message is wrong, of course.

提交回复
热议问题