-1

I am working on a ARC based project. I am just wondering what is the main use of self ?

I am having an array as follows

@property(strong,nonatomic)NSMutable *dataArray;

I am initializing the array as follows

-(void)viewDidLoad{

 self.dataArray=[[NSMutableArray alloc]initWithObjects:@"one",@"two",nil];  //first method

}

or

-(void)viewDidLoad{

 dataArray=[[NSMutableArray alloc]initWithObjects:@"one",@"two",nil];  //second method

}

Can anyone tell me what is the difference between my first and second method ?

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
Raj
  • 1,119
  • 1
  • 15
  • 32
  • May I know what is wrong in my question ? Someone down voted it – Raj Oct 10 '12 at 11:45
  • you could easily find answer just by using google – Andrey Chernukha Oct 10 '12 at 11:47
  • The basic idea is that you are able to use the second method because in addition to the property you have also declared an instance variable `NSArray *dataArray;` in your header file. Then when you try to alloc/init dataArray is the iVar and self.dataArray is the property. – Mick MacCallum Oct 10 '12 at 11:49
  • I tried many links , but couldn't get the proper answer .But at last I found a proper link explaining everything in detail. By that time I had already posted my question – Raj Oct 10 '12 at 11:49
  • So you're welcome to synthesize what you've read and post it as an answer to your own question. (Please don't paste the raw link only). – Cyrille Oct 10 '12 at 11:51
  • possible duplicate of [Difference between self.ivar and ivar?](http://stackoverflow.com/q/4142177/), [Difference between self and normal variable](http://stackoverflow.com/q/536388/), [Properties and accessors](http://stackoverflow.com/q/6085080/), [Ivar property, access via self?](http://stackoverflow.com/q/4088801/), [When to access properties with self](http://stackoverflow.com/q/4271657/), [What is the (style) difference between “self.foo” and “foo” when using synthesized getters?](http://stackoverflow.com/q/3494157/), and [many more](http://stackoverflow.com/search?q=%5Bobjc%5D+self.ivar) – jscs Oct 10 '12 at 17:56

2 Answers2

4

The first invocation: self.dataArray = ... invokes the accessor method of self, like this: [self setDataArray:...]

The second invocation: dataArray = ... defines the content of the instance variable (ivar) named dataArray.

That's not the same thing.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Can you expand on what the differnce is between the effect of self.dataArray = ... & dataArray = ...? – nerak99 Oct 10 '12 at 16:38
1

After lot googling I found the answer to my question ,

@property (nonatomic, retain) NSDate *timestamp;

Objective C will generate the getter and setter as follows

(NSDate *)timestamp
{
   return timestamp;
}

(void)setTimestamp:(NSDate *)newValue
{
   if (timestamp != newValue)
   {
      [timestamp release];

      timestamp = [newValue retain];
   }
}

The setter method can be invoked only as follows

self.timestamp = [NSDate date];

where as

timestamp=[NSDate date];

does not invoke the setter method.

Without the self object and the dot we are no longer sending an object a message but

directly accessing the ivar named timestamp.

Conclusion: When we don't use self , the old value won't be released ,because the setter

method won't be invoked i.e Prior to ARC.But in ARC I am not pretty sure about this.As far

as I heard , With ARC either way of setting the ivar is correct as far as memory management

is concerned.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
Raj
  • 1,119
  • 1
  • 15
  • 32
  • I believe your conclusion is an incorrect assumption under ARC. Before ARC you are correct, and due to that it was good practice to always use the setter method. With ARC either way of setting the ivar is correct as far as memory management is concerned (http://stackoverflow.com/questions/7986954/is-self-ivar-necessary-for-strong-properties-with-arc). Also, this is a good reason for naming ivars different than property names, so you con easily distinguish between using a property and ivar. – Ryan Oct 10 '12 at 12:39
  • oh ok . I dint know this . I will check it once and then remove the conclusion part. – Raj Oct 10 '12 at 12:42
  • Here is the question I found helpful when I was looking into this initially. http://stackoverflow.com/questions/10038302/objective-c-custom-setter-with-arc – Ryan Oct 10 '12 at 15:00
  • "Objective C will generate the getter and setter as follows" only if you synthesize the property – newacct Oct 10 '12 at 20:00