One Quickie


Apple's recommended retain/release technique (NSObject->General)
This way if "get" performance is important:
- (NSString*) title
{
    return (title);
} // title

- (void) setTitle: (NSString *) newTitle
{
    [title autorelease];
    title = [newTitle copy];
} // setTitle
For maximum safety in the face of threads, use this:
- (NSString *) title
{
    return [[title retain] autorelease];
} // title
This puts title into the current thread's autorelease pool, so title is protected from being destroyed by someone else in another thread.
- (void) setTitle: (NSString *) newTitle
{
    // use a mutex or NSLock to protect this
    if (title != newtitle) {
        [title release];
        title = [newTitle copy];
    }
} // setTitle
By making a copy of the object, you're protected from another thread changing the value underneath you.



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com