-5

I have searched a lot but get only difference in definitions of non-atomic, retain, copy parameters. I want exact explanation which I am not getting yet.

Please explain it with example :why we are using them and how they work and role of reference counting in them?

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
appleGal
  • 1
  • 3

1 Answers1

1

In nonatomic, no guarantees are made for returning a whole value from the getter/setter, if some setter activity is going on any other thread.

Thus, nonatomic is considerably faster than atomic, and never thread-safe


In retain explicitly state that you want to maintain a reference of the object or you want to be the owner of the object, and you must release it before it will be deallocated. It will increase the reference count by 1.

-(void)setString:(NSString*)newString{
     
    [newString retain];
     
    [string release];
       
    string = newString;
    
}

The copy is often used with strings, since making a copy of the original object ensures that it is not changed whilst you are using it.

-(void)setString:(NSString*)newString{
 
      if(string!=newString){
                 
          [string release];
                 
          string = [newString copy];
               
     }
}

While retain simply increments the retain count of an object, copy creates a new one one with its own retain count.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • thanks but suppose i have A which is of retain type and B which is of copy type and if i am assigning a new variable C in firstly to A and then to B .,...then what will happen with the values of A and B.How they will work in it. – appleGal Mar 11 '13 at 10:03
  • As you can see the setters code in my answer, A will retain C's value and B will copy C's value. As you seems to be a learner. I won't say don't learn Memory-Management, you should know but take a dive into ARC, where most of this things are done by compiler for you. Once you feel good about obj-c, then start learnign memory-management and programatically GUI creation. – Anoop Vaidya Mar 11 '13 at 10:18
  • if this solved your question, kindly accept the answer :) – Anoop Vaidya Mar 11 '13 at 12:34