Objective C - Assign, Copy, Retain

IosObjective CMacosMemory ManagementAttributes

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

Updated Answer for Changed Documentation

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

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).

Solution 2 - Ios

The Memory Management Programming Guide from the iOS Reference Library has basics of assign, copy, and retain with analogies and examples.

> copy Makes a copy of an object, and returns it with retain count of 1. If you copy an object, you own the copy. This applies to any method that contains the word copy where “copy” refers to the object being returned. > > retain Increases the retain count of an object by 1. Takes ownership of > an object. > > release Decreases the retain count of an object by 1. Relinquishes > ownership of an object.

Solution 3 - Ios

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"First",@"Second", nil];
NSMutableArray *copiedArray = [array mutableCopy];
NSMutableArray *retainedArray = [array retain];

[retainedArray addObject:@"Retained Third"];
[copiedArray addObject:@"Copied Third"];

NSLog(@"array = %@",array);
NSLog(@"Retained Array = %@",retainedArray);
NSLog(@"Copied Array = %@",copiedArray);

array = (
    First,
    Second,
    "Retained Third"
)
Retained Array = (
    First,
    Second,
    "Retained Third"
)
Copied Array = (
    First,
    Second,
    "Copied Third"
)

Solution 4 - Ios

  1. assign
  • assign is a default property attribute
  • assign is a property attribute tells the compiler how to synthesize the property’s setter implementation
  1. copy:
  • copy is required when the object is mutable
  • copy returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments
  • you need to release the object when finished with it because you are retaining the copy
  1. retain:
  • specifies the new value should be sent “-retain” on assignment and the old value sent “-release”
  • if you write retain it will auto work like strong
  • Methods like “alloc” include an implicit “retain”

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSabha BView Question on Stackoverflow
Solution 1 - IosJoshua NozziView Answer on Stackoverflow
Solution 2 - IosLarry HippView Answer on Stackoverflow
Solution 3 - IossrivasView Answer on Stackoverflow
Solution 4 - IosChen RuiView Answer on Stackoverflow