0

I am a newbie in Objective-C and would like to know more about the non-arc based programming especially to override the setters for assign, retain and copy. Could someone please help me out. And also please brief on the process.

1 Answers1

3

Here are brief explanations of each:

assign is the default and simply performs a variable assignment. It does not assert ownership, so the object pointed to by the property's pointer may disappear at any time if no others have asserted ownership themselves through retain or other means.

- (void) setAssignProperty:(id)newValue
{
    self->assignProperty = newValue;
}

retain specifies the new value should be sent -retain on assignment and the old value sent release. Retain is also know as strong. Method retain increases retainCount on the object (object won't be released until you retainCount is 0).

-(void)setRetainProperty:(id)newValue
{
    if (retainProperty != newvalue)
    {
        [retainProperty release];
        retainProperty = [newValue retain];
    }
}

copy specifies the new value should be sent -copy on assignment and the old value sent release. Copy creates a new instance of the object.

-(void)setCopyProperty:(id)newValue
{
    if (copyProperty != newvalue)
    {
        [copyProperty release];
        copyProperty = [newValue copy];
    }
}

I would also like to note that there is almost no reason not to use arc.

Dobroćudni Tapir
  • 3,082
  • 20
  • 37
  • Yeah I know using ARC will reduce a lot of work and is more clean. But I would like to clear myself the concept of memory management. And I also wanted to know what exactly does retain and copy do. – user3008132 Dec 19 '13 at 09:57
  • Good answer, and I admire the askers desire to learn exactly how the memory management works. It helps when using ARC to know what happens below – Accatyyc Dec 19 '13 at 10:45
  • It also helps with understanding foundation objects that aren't ARC friendly. Memory Management should be required when new people are learning Cocoa. Props :) – migs647 Mar 24 '14 at 19:47