Property vs Instance Variable (iVar) in Objective-c [Small Concept but Great Understanding..]

Do you use properties or instance variables to store something inside an object? Let’s understand what is better and when.
I think the subject is much less arguable if it’s about getting/setting some data from outside the class (right, properties should be used). So in this post we will focus on private and protected class members.

Does it matter?

It might sound not that important – why can’t we just make a random decision and stick to it? Well, properties / ivars are something that is used all the time. I think it’s a good practice to sometimes pay attention to how we do common tasks. It’s natural, and it’s a good way to increase efficiency as well – even a slight improvement sums up to something quite significant within time. 

Performance

Obviously, using properties will cost you some CPU because additional methods get called. But this is so insignificant in 99.99% of the cases! And for the rest 0.01% – use ivars, but be aware that the improvement you get is almost nothing compared to other optimizations you usually can add.

Description

To declare a variable in a objective-c class, we have the following two ways:

  • Property


@interface Photo : NSObject
@property (nonatomic, strong) NSString *photographer;
@end


  • Instance Variable (iVar)

@interface Photo : NSObject {
    NSString *photographer;
}
@end


Then, what’s the difference?

Differences

private vs public

For a private/protected variable, use iVar; for a public variable, use property. If you want to use the benifit of property attributes for a private variable, like retain, nonatomic etc., declare the property in the implementation file as a private property.
For an iVar, you can use @private@protected and @public. But these attributes only influence the access of its subclass, and has nothing to do with the access ability of its instances. Go to here for reference.

usage

Directly use an iVar inside the class, for example, photographer. But use self. for a property, for example, self.photographer.

performance

iVar is faster than property, as property will call the getter or setter functions. When you declare a property, the compiler will add getter and setter functions for the property.

@synthesize for property

UPDATE:Just read this paragraph, omit the paragraphs behind.
@synthesize is only for property, not iVar. It will help the property generate an instance variable, the getter and setter accessors for the property and use the generated instance variable in the getter and setter accessors. For every property, complier will automatically synthesize it using _propertyName. The auto synthesize is the same as you add @synthesize propertyName = _propertyName. If you only add @synthesize propertyName, it is the same with @synthesize propertyName = propertyName, and if you define an iVar with the name propertyName, they will be synthesized. That is to say, in the accessors (getter and setter) of property propertyName, it will use iVar propertyName.
Note for property inheritance: If the super class has a property declared as public, the subclass can inherit the property. But the auto synthesize mechnism will not sythesize the property automatically, and you can only use the getter and setter accessors of the superclass by using dot operation. If you want to synthesize manually, Be Carefull! You should avoid using the same iVar as them in the superclass. This will lead to an error message “property ‘propertyName’ atemping to use instance variable ‘InstanceVariableName’ declared in super class ‘SuperClassName’”.
If you inherit a property from a protocol, you mush synthesize it, as the protocol only declares it, not synthesize it.
Omit the following paragraphs in this section
If you add @synthesize photographer in the implementation, compiler will automatically add an iVar photographer
and _photographer to the class. You can directly use photographer
or _photographer instead of self.photographer to get or set the value. The iVar method is faster, but keep in mind that it will not call the getter or setter method.
If you declare the class like this and don’t @synthesize photographer:


@interface Photo : NSObject {
  NSString *photographer;
}
@property (nonatomic, strong) NSString *photographer;
@end

There are actually two photographer variables in the class: when you use photographer directly, you are using the iVar, and when you use self.photographer, you are using the property.
However, when you use @synthesize photographer in the implementation file, the compiler will add photographer variable for the property. That is to say, photographer will be the property, and the iVar will not be usable.

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. If you are looking for developing iOS Application Development that could clear the vision for that you need an iOS app development company to catch up to your expectations for the product.

    ReplyDelete
  3. It's actually an important blog entry. This blog momentarily clarifies about APP Development. This is one of the unmistakable programming dialects generally utilized. A debt of gratitude is in order for sharing this decent article Mobile App Development Services

    ReplyDelete
  4. Chuilaiandcocpa is a leading immigration law firm specialising in all areas of UK immigration law. Our immigration lawyers will remove the stress from the immigration process, maximising your chances of a successful outcome. If you are planning to visit, reside or extend your stay in the UK, our specialist advisers, many of whom are also qualified as UK immigration solicitors, are here to help you and your family with our tailored immigration advice chuilaiandcocpa.com.

    ReplyDelete

Post a Comment

Popular posts from this blog

iOS Architecture

setNeedsLayout vs layoutIfNeeded Explained