Home > 2010 Fall DPS913 > Properties in Objective-C

Properties in Objective-C

September 16, 2010 Leave a comment Go to comments

A declared property provides a convenient syntax for the creation of accessors in Objective-C.

.

A declared property provides a convenient syntax for the creation of accessors (i.e. getters and setters) for instance variables in a class. The following information is valid for iPhone OS 3.0 and later SDK versions, and for 64-bit Mac OS X 10.5 and later apps.

.

Declaring a property

In your .h interface, assume that you need a string instance variable. It is declared as a property, as follows:

@property (nonatomic, retain) NSString *myString;

Then, in your .m implementation, just below the “@implementation…” statement, write this code (which creates the instance variable and its accessors at compile time):

@synthesize myString;

Finally, in your dealloc method, write this code to perform memory management on the declared property:

[myString release];

Historical note: Older versions of Objective-C and the runtime required you to declare an instance variable, in addition to the property declaration. (The instance variable name matched the property name.) Therefore, you will see old code samples that follow this pattern. However, you do not do this any longer when coding for the newer (current) versions of Objective-C and the runtime.

.

Declaring a property for an outlet

Apple recommends that outlets be declared using the declared property feature. Therefore, you follow the same rules as discussed in the previous section. Then, there are two additional considerations when declaring a property for an outlet:

First, as covered earlier, include the IBOutlet type qualifier when you declare the property in your .h interface. Insert “IBOutlet” before the type name, as follows:

@property (nonatomic, retain) IBOutlet UILabel *myLabel;

Then, in your .m implementation, write this code in the viewDidUnload: method (which improves the effectiveness of memory management tasks), placing it before the call to super:

self.myLabel = nil;

.

Using a property in your code

When you use your property in your code, add the “self.” prefix.

Using the “self.” prefix unambiguously means that you are referring to the property. If you do not use “self.”, it means that you are referring to an instance variable.

For example, if you want to set the “text” property of the “myLabel” property you created in the previous section, write this code:

self.myLabel.text = @”Hello, world!”;

An additional example: If you want to get the length of the “myString” property you created in the top section above, write this code:

NSLog(@”length %d”, [self.myString length]);

.


Advertisements
Categories: 2010 Fall DPS913
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: