Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't property observers be added to lazy properties?

Tags:

ios

swift

I watched a tutorial on swift basics and a passing comment was made that property observers can't be added to lazy properties, but no reason was given. Why is this the case?

like image 336
NSologistic Avatar asked Apr 09 '15 16:04

NSologistic


1 Answers

The docs do confirm this:

You can add property observers to any stored properties you define, apart from lazy stored properties

source

but they do not provide a rationale.

I would guess that property observers must access the property they are observing to attach itself as an observer although it does not 'feel' like you are. Again, this is just a guess, but if I am correct, then obviously having a lazy property is irrelevant. Lazy properties are computed when first accessed and if using an observer accesses that property then it would be computed immediately, which completely defeats the purpose of it in the first place.

like image 51
Firo Avatar answered Sep 22 '22 11:09

Firo