Can I use instance variables created for auto-synthesized properties?

Currently, I can write a class as follows:

@interface Foo @property(assign) float bar; @end @implementation Foo - (void) someMethod { _bar = 4; } @end 

Conveniently, I can leave the list of @synthesize templates, and if I get used to underscore notation, I get a good readable rule that everything with the name _foo is an instance variable. Can I use automatically generated instance variables? I mean, maybe they should be invisible even to the author of the class?

+4
source share
2 answers

Yes, it is absolutely normal to use these variables.

@synthesize requirement was generally convenient: now @synthesize xyz implicitly inserted - this is the only difference. Compiler designers argued that since they can uniquely identify situations where you want to synthesize accessors or situations when providing custom implementations, it’s wise to stop asking you for explicit @synthesize .

+3
source

Can I use instance variables created for automatically synthesized properties?

Absolutely

Conveniently, I can leave the @synthesize template list, and if I get used to underscore notation, I get a good readable rule that everything called _foo is an instance variable.

Of course, you also have the option of specifying a name using @synthesize foo = f00; .

Can I use automatically generated instance variables? I mean, should they be invisible even to the author of the class?

Nope. They need to be internally accessible. This is less common if you use ARC, but the usual case when you access them directly is initialization and destruction (cases where access methods should not be used).

+2
source

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


All Articles