Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - Assign, Copy, Retain

I'm new to Objective C. I have basic knowledge in C, including the concept of pointers. I have two basic questions:

  1. Can someone explain the difference between assign,copy, and retain with some analogy?
  2. How do you handle a function which returns pointer variable, and how do you perform messaging through a return pointer?
like image 507
Sabha B Avatar asked Dec 22 '10 15:12

Sabha B


People also ask

What is difference between assign and retain?

Assign creates a reference from one object to another without increasing the source's retain count. Retain creates a reference from one object to another and increases the retain count of the source object.

What is the difference between copy and retain?

Retain increases the retain count of an object by 1 and takes ownership of an object. Whereas copy will copy the data present in the memory location and will assign it to the variable so in the case of copy you are first copying the data from a location assign it to the variable which increases the retain count.

What is retain in OBJC?

You send an object a retain message when you want to prevent it from being deallocated until you have finished using it. An object is deallocated automatically when its reference count reaches 0 . retain messages increment the reference count, and release messages decrement it.

What is assign in Objective C?

assign -assign is the default and simply performs a variable assignment -assign is a property attribute that tells the compiler how to synthesize the property's setter implementation -I would use assign for C primitive properties and weak for weak references to Objective-C objects.


1 Answers

Updated Answer for Changed Documentation

The information is now spread across several guides in the documentation. Here's a list of required reading:

  • Cocoa Core Competencies: Declared property
  • Programming with Objective-C: Encapsulating Data
  • Transitioning to ARC Release Notes
  • Advanced Memory Management Programming Guide
  • Objective-C Runtime Programming Guide: Declared Properties

The answer to this question now depends entirely on whether you're using an ARC-managed application (the modern default for new projects) or forcing manual memory management.

Assign vs. Weak - Use assign to set a property's pointer to the address of the object without retaining it or otherwise curating it; use weak to have the property point to nil automatically if the object assigned to it is deallocated. In most cases you'll want to use weak so you're not trying to access a deallocated object (illegal access of a memory address - "EXC_BAD_ACCESS") if you don't perform proper cleanup.

Retain vs. Copy - Declared properties use retain by default (so you can simply omit it altogether) and will manage the object's reference count automatically whether another object is assigned to the property or it's set to nil; Use copy to automatically send the newly-assigned object a -copy message (which will create a copy of the passed object and assign that copy to the property instead - useful (even required) in some situations where the assigned object might be modified after being set as a property of some other object (which would mean that modification/mutation would apply to the property as well).

like image 84
Joshua Nozzi Avatar answered Sep 19 '22 10:09

Joshua Nozzi