1

I do not quite understand the way of declaring instance variable and property. Can someone explain in detail the difference of the two codes below? In the second method, if I use _name for instance variable, is it the same function as the way declaring name in first code? Thanks!

First Code:

//  OrderItem.h
#import <Foundation/Foundation.h>

@interface OrderItem : NSObject

{

  @public NSString *name;

}

-(id) initWithItemName: (NSString *) itemName;

@end


//  OrderItem.m
#import "OrderItem.h"

@implementation OrderItem

-(id) initWithItemName: (NSString *) itemName {

     self = [super init];

      if (self) {

          name = itemName;

         NSLog(@"Initializing OrderItem");
   }

     return  self;

   }

@end

Second Code:

//  OrderItem.h
#import <Foundation/Foundation.h>

@interface OrderItem : NSObject

 @property (strong,nonatomic) NSString *name;

-(id) initWithItemName: (NSString *) itemName;

@end


//  OrderItem.m
#import "OrderItem.h"

@implementation OrderItem

-(id) initWithItemName: (NSString *) itemName {

     self = [super init];

      if (self) {

          _name = itemName;

         NSLog(@"Initializing OrderItem");
   }

     return  self;

   }

@end
2
  • Not an exact duplicate (though I think there is one; need to find it), but see stackoverflow.com/questions/12899305/… Commented Aug 7, 2016 at 13:26
  • Mike there is lot of answer for your question in Stackoverflow.Even I can give answer now but everywhere you can see the solution according to your question. Commented Aug 7, 2016 at 13:50

2 Answers 2

1

In the first case you have declared an instance variable (usually called an ivar in Objective-C).

In the second case you have declared a property. A property is a set of two methods, a getter and a setter, usually accessed using dot notation, e.g. self.name. However, an ivar is automatically synthesized for the property with the name _name. That instance variable is what you are accessing in your init.

You can actually change the name of the ivar using @synthesize name = _myName or not have it at all (if you declare the getter and setter manually, no ivar will be synthesized).

Objective-C properties are a rather complicated topic so don't worry if you don't understand it immediately.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Sulthan, thank you very much! In practice, I prefer the second way, so if I code in the second way, there should be no problem, right?
@Mike Properties are generally preferred over instance variables.
@Sulthan Hi Sulthan, got it. I have accepted your answer. Have a nice Sunday!
1

Properties are public which means that other classes can read and write them (even classes that aren't subclasses of the class that declares the property). In addition to that, properties provide a getter and a setter method (mutator methods). The getter of a property gets called every time you access the property

NSString *aName = self.name;

Whereas the setter is accessed every time you write or assign to a property.

self.name = @"Some name";

Instance variables (or ivars) are, by default, only visible for the class that declares it and its subclasses (also known as being encapsulated by their class). You can change this default behavior when you add the keyword @public to your ivar declaration though.

9 Comments

@user3182143 Thanks! I hope my answer helped you a bit.
Hi Marcel, thanks a lot! According to the function of the code, in practice both ways have no problem, right?
Well, ivars are public too. Or better said, there is no concept of public or private in Objective-C. If you declare your ivars in the header, they are accessible externally, too. There are only accessed using the -> notation.
@Mike Yes, you can use both ways. Although, I would recommend to use properties only if it's necessary since they are public to other classes as long as they are declared in your header. You shouldn't disclose too much information to other classes without having to.
@MarcelVoss There is absolutely zero difference between ivars and properties when speaking about accessibility.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.