Overriding setters for assign, retain and copy

1k views Asked by At

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

There are 1 answers

3
Dobroćudni Tapir On BEST ANSWER

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.