Using @property and @synthesize with implicit ivar creation

Ok, so lately I got this supposedly bad habit of declaring my class properties with @property create getters and setters with @synthesize and just use them anywhere I need them, like self.name without reference to internal ivar ( let @synthesize do its job)

Now, of course, this does not allow me to access the internal ivar (for example, name_ ), but from what I have encoded so far, I really do not need it. For read-only properties, I do not use @synthesize , but I implement getter myself.

Everything seems fine, but for some reason I have the feeling that it is wrong, because all the open source libraries that I looked at also declared ivar and used it in all the code. @property + @synthesize and no ivar is certainly a lazy choice, but what are the disadvantages? Can anyone give me some advice?

In addition, I read that, as a general tip, you can use self.propertyName anywhere in your class code except for the dealloc and init methods. But until you make sure that the object is initialized:

 -(id) init { if( (self=[super init] )) { } return self; } 

and you delete all the observers with the key before calling [super dealloc] everything should be fine. Right?

+6
source share
2 answers

well @synthesize will create an internal ivar with the same name as the property:

 @synthesize name; - (void) dealloc { [name release], name = nil; [super dealloc] } 

Or give the inner ivar a different name:

 @synthesize name = _name; - (void) dealloc { [_name release], _name = nil; [super dealloc] } 

I use self.name in init, but not in dealloc and it seems to work fine.

+5
source

According to the version of the Objective-c runtime that you create for: Modern or Legacy .

  • The modern version supports "synthesis of instance variables for declared properties." i.e. you do not need to define an instance variable for each @property . Runtime will take care of creating for you. By default, ivar will be called the same as @property . You can change the ivar name using the format @synthesize propertyName = iVarName;

  • The legacy version does not support ivar synthesis, so you must define ivar to store your @property .

IPhone applications and 64-bit programs on Mac OS X v10.5 and later use the latest version of the runtime. You should assume that every other platform uses an outdated version.

What are the disadvantages? Well, the biggest one is portability. Without defining ivars, your code can run on fewer platforms, for example. it will not work on OSX 10.4. You also lose control / flexibility and can inadvertently trigger name conflicts with sub- and superclasses.

It is important to remember that you must deactivate the properties you synthesized with @synthesize . Properties will not be automatically released - you must release any synthesized property that is not labeled assign (do not release any assigned properties assigned).

NB For an extra loan, you can clear your read-only properties. There is little reason to write your own accessor and not use @synthesize . Just define your property as read-only and optionally to call getter @property(getter=isIntReadOnlyGetter, readonly)

+5
source

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


All Articles