Initializing property via closure

前端 未结 2 1441
醉酒成梦
醉酒成梦 2021-01-15 01:48

I\'ve observed that people sometimes using closures to initialize properties. e.g. instead of

lazy var test1: String = String(\"a string\")
<
2条回答
  •  情歌与酒
    2021-01-15 02:29

    These two do the same work. Closure initialization comes handy when you need extra code to configure property object. E.g.:

    lazy var point: CGPoint = {
        let x = ...
        let y = ...
        return CGPoint(x: x, y: y)
    }()
    

提交回复
热议问题