The difference between using properties and not accessing ivars

Specific performance and behavioral differences using properties or direct access to ivars.

For global variables What is the difference between using this parameter:

@interface myClass (){ UIImageView *myView; } -(void)loadView{ [super loadView]; myView = [[UIImageView alloc] initWithFrame:CGrectMake(0,0,100,100)]; } 

And by doing this:

  @interface myClass (){ } @property (nonatomic, strong) UIImageView *myView; @synthesize myView = _myView; -(void)loadView{ [super loadView]; myView = [[UIImageView alloc] initWithFrame:CGrectMake(0,0,100,100)]; } 

What advantages do we have with each approach? What are the reasons to recommend always using properties?

+6
source share
4 answers

In the first case, your instance variable (or ivar) myView is private to the class and cannot be accessed by another class.

In the second case, you provided a property that allows other classes to access your ivar through synthesized accessors. An alternative to declared properties is to create your own access methods. The @synthesize designation does this for you.

See Apple documentation for declared properties

+4
source

ALWAYS create @property for each data member and use self.name to access it in the entire implementation of the class. NEVER directly contact your own data members.

The following are some reasons for using properties:

  • Properties provide access restrictions (for example, read-only)
  • Properties provide a memory management policy (save, assign)
  • Properties (rarely) are used as part of a thread safety strategy (atomic)
  • Properties provide the ability to transparently implement custom setters and getters.
  • Having one way to access instance variables improves code readability.

You can also check: Code Commandments: Best Practices for Objective-C Coding

+4
source

Synthesize allows you to use the getter and setter methods, which are called automatically depending on whether you are trying to read or write a value. For the myView property:

 myView = newView1; // using direct ivar access myobject.myView = newvew1; // eq [myobject setMyView:newvew1]; where setMyView: is generated for you automatically with respect to assign/retain, the same for reading: newvew1 = myobject.myView; // newvew1 = [myobject myView:newvew1]; 

the generated receiver / setter names are configured using setter = / getter = if you do not need the installer to use readonly.

You cannot forbid other classes to use the synthesized getter and setter, ivars are protected by @project by default, and if you want to give other classes access to ivars, you can declare them in @public:

 @interface myClass (){ UIImageView *myView; // this is protected @public UIImageView *myPublicView; // this is public } 
+3
source

In the first example, you access your ivar directly and change its contents. In your second example (property), ivar was created automatically to return the property, and all your calls to set and receive the property are sent as messages (for example: [self setMyView:[[UIImageView alloc] initWithFrame:CGrectMake(0,0,100,100)]]; ). Access methods are also created automatically. This means that you are now following the KVC / KVO protocols. See here for more on the benefits of this design .

+1
source

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


All Articles