IOS: The ambiguous behavior of the synthesis of properties. Legacy

I use AppCode , and he noted an interesting situation in the code of a very large project. Pre-arc

A subclass defines and synthesizes a property called a delegate. Actually the declaration of ownership was commented! But the operator @synthesize delegate = delegate_; left behind.

The code is compiled, apparently because the base class defines and synthesizes a property, also called a delegate, and synthesizes it using a support variable of the same name: @synthesize delegate = delegate_;

My question is: what happens to the message sent to the delegate in

a) base class methods and

b) in subclass methods.

AppCode places the AppCode expression in a subclass as an error:

Property delegate accessors have already been synthesized using the instance variable delegate _

+3
source share
1 answer

The @synthesize directive is a shorthand for creating access methods and ivar according to the specifications (atomicity, memory management) of a property with the same name. Given that re-synthesizing a property in a subclass (without re-declaring it) works just like redefining access methods - subclasses are used instead of the subclass. Since implementations are created by the compiler in both cases, there is no noticeable difference in behavior.

The only difference is that the synthesized Ivar has the same visibility as @private Ivar, so subclasses cannot access it, including to use it as a base variable for a property. This means that the re-synthesis in the subclass must use a different ivar name. If the superclass has @synthesize wildHorses = wildHorses_; , then the compiler requires the subclass to do something like @synthesize wildHorses = equusFerus; . *


* If the superclass uses the default name for the created ivar, @synthesize wildHorses; , then the subclass should still synthesize the new variable: @synthesize wildHorses = wildHorses_;

+5
source

All Articles