swift - wondering use of computed property -
before.
class drawingview: uiview { var arccenter = cgpoint(x: frame.midx, y: frame.midy) // error }
after.
class drawingview: uiview { var arccenter: cgpoint { return cgpoint(x: frame.midx, y: frame.midy) } }
i know why error occur, can't understand why second codes not makes error.
the first form not computed property.
it stored property default value.
it uses self
causes error because @ moment default value going assigned instance not guaranteed instantiated.
an alternative computed property – computed @ moment it's called – is lazy instantiated property.
lazy var arccenter: cgpoint = cgpoint(x: self.frame.midx, y: self.frame.midy)
unlike stored property default value assigned when property accessed first time.
Comments
Post a Comment