Using self in callback passed to super.init

I have a base class. In this base class, I take some callbacks in the init method. In these callbacks, I would like to refer to a derived class self. However, Swift complains that the link is lambdas selfbefore super.init. Of course, I know that in fact the base class does not actually call back inside super.init, and even if that were the case, it would not be clearly illegal, as it would not have been before super.init.

How to pass a callback super.initthat refers to self?

0
source share
2 answers

I managed to get around this pointless restriction by paraphrasing the class a bit. Instead of just passing the constructor the data it needs, there is instead a getter for the data that you just need to call exactly once with the constructor of the superclass. A variable that stores this data, instead of being unchanged and correct, is now initialized to zero (i.e., Left uninitialized), and then initialized later via getter, and then there is another computed property just for receiving.

So, now anyone who tries to read a class will be completely embarrassed by the useless mockery of the dot, but actually has the desired semantics.

0
source

self, super.init. self . ( factory, ).

+1

Source: https://habr.com/ru/post/1692743/


All Articles