Objective-C: Working with Properties

In this installment I will talk about Objective-C and properties. In Objective-C, you simply manipulate pointers to point to memory, and changing where the pointers refer to, and usually involves retaining or releasing memory to prevent leaks.

Properties are Objective-C’s 2.0 version of managing getters and setters for an object’s methods, and the language has provided this shortcut with the aid of making memory management easier.

Creating Properties

Creating properties, you use the @property directive in the header file declares a property, and the @synthesize directive in the implementation file asks the compile to generate accessors. If you use @dynamic instead of @synthesize you tell the compiler you will explicitly provide the accessor methods (getters and setters).


‘Retain Attribute’
The first property attribute, ‘Retain’ lets you manage the memory yourself, and you have to release the variable memory allocations, via something like [temp1 release]; So when we are in the method, the allocation count is 2, one for when the first allocation occurs and then when you refer to the ‘Retain’ attribute, the count is incremented.

‘Assign Attribute’
This is like assigning a pointer to an object without increasing its retain count, but generally not commonly used.

‘Copy Attribute’
If you want an independent copy of an object rather than creating a pointer to the same object, you use copy, so the setter creates a new object and the original object is duplicated, and is referred to as a Deep copy (as opposed to a Shallow copy which duplicates an object’s references to the same instance variables).


@interface aClass : NSObject {

NSString * myString;

@property(copy) NSString *myString;


#import "aClass.h"

@implementation aClass

@synthesize myString;


This entry was posted in General and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s