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

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -