1.

Write the difference between assign and retain keywords.

Answer»
  • Assign: With Assign, a reference is created from one object to another without increasing the source's RETAIN count (a number that KEEPS track of how MANY objects are “holding onto" another object). It does not copy or retain the value but assigns it directly to the instance variable.

Example: 

if (object != object) {  [object release];  object = nil;  object = object;  }

Here, Assign will generate a setter that assigns the value to the instance variable directly, RATHER than copying or retaining it.

  • Retain: Using this method, you create a reference from one object to another and INCREASE the retain count of the source object.

Example: 

if (object != object) {   [object release]; object = nil;   object = [object retain];   }

A retain message prevents an object from being deallocated until you are done using it.



Discussion

No Comment Found