Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Accessor Methods

For simple object values, getters and setters can be written in one of the following three ways:

  1. Getter retains and autoreleases the value before returning it; setter releases the old value and retains (or copies) the new value.

  2. Getter returns the value; setter autoreleases the old value and retains (or copies) the new value.

  3. Getter returns the value; setter releases the old value and retains (or copies) the new value.

In technique 1, values returned by the getter are autoreleased within the calling scope:

- (NSString*) title
{
    return [[title retain] autorelease];
}
 
- (void) setTitle: (NSString*) newTitle
{
    if (title != newTitle) {
        [title release];
        title = [newTitle retain]; // or copy depending on your needs
    }
}

As with values manufactured by class convenience methods, the returned object is autoreleased in the current scope and thus remains valid if the property value is changed. One issue with this technique is performance. If you expect your getter method to be called frequently, the added cost of retaining and autoreleasing the object may not be worth the performance cost.

Technique 2 also uses an autorelease technique, but this time does so in the setter method:

- (NSString*) title
{
    return title;
}
 
- (void) setTitle: (NSString*) newTitle
{
    [title autorelease];
    title = [newTitle retain];
}

The performance of technique 2 is significantly better than technique 1 in situations where the getter is called much more often than the setter.

Technique 3 avoids the use of autorelease altogether:

- (NSString*) title
{
    return title;
}
 
- (void) setTitle: (NSString*) newTitle
{
    if (newTitle != title) {
        [title release];
        title = [newtitle retain];
    }
}

The approach used by technique 3 is good for frequently called getter and setter methods. It is also good for objects that do not want to extend the lifetime of their values, such as collection classes. However, because of the potential dangers of invalidating objects prematurely, use of this technique should be used sparingly and well documented.

Value Objects and Copying

It is common practice in Objective-C code to copy value objects—objects that represent attributes. C-type variables can usually be substituted for value objects, but value objects have the advantage of encapsulating convenient utilities for common manipulations. For example, NSString objects are used instead of character pointers because they encapsulate encoding and storage. Despite NSString functionality, the role played by NSString objects parallels the role played by character pointers.

When value objects are passed as method arguments or returned from a method, it is common to use a copy instead of the object itself. For example, consider the following method for assigning a string to an object’s name instance variable.

- (void)setName:(NSString *)aName
{
    [name autorelease];
    name = [aName copy];
}

Storing a copy of aName has the effect of producing an object that is independent of the original, but has the same contents. Subsequent changes to the copy don’t affect the original, and changes to the original don’t affect the copy. Similarly, it is common to return a copy of an instance variable instead of the instance variable itself. For example, this method returns a copy of the name instance variable:

- (NSString *)name
{
    return [[name copy] autorelease];
}


< Previous PageNext Page > Hide TOC


Last updated: 2008-02-08




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice