discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Setter Gettor method style


From: Martin Häcker
Subject: Setter Gettor method style
Date: Sat, 3 Aug 2002 11:27:08 +0200

Hi there!

Sorry if I spring in here, but I'd like to ask you about a topic wich was discussed in depth on the Apple Cocoa Mailing list lately:

The question was how getter setter methods are written and they came to the conclusion that these are the way's:

--- snip ---
Method 1: The following pair is good if performance of "get" is important (say you expect it to be called it thousands of times a second):

 - (NSString *) title {
         return title;
 }

 - (void) setTitle: (NSString *)newTitle {
         [title autorelease];
         title = [newTitle copy];
         // or retain, depending on object & usage
 }

 (as an optimization, you can do "if (title != newTitle)" in the set method).


Method 2: Otherwise use the following pair, where the return value is autoreleased in the scope of the caller, which is more correct. This is also more (but not fully) thread-safe as the returned value is autoreleased in the calling thread.

 - (NSString *) title {
         return [[title retain] autorelease];
 }

 - (void) setTitle: (NSString *)newTitle {
         if (title != newTitle) {
                 [title release];
                 title = [newTitle copy];
                 // or retain, depending on object & usage
         }
 }
Ali Ozer
--- snap ---

All this because of complicated manners which you can read about in this thread:
http://cocoa.mamasam.com/COCOADEV/2002/07/2/41217.php

Now, what I'd like to ask you is what you think about it and what style is used in the GnuStep frameworks?

Thanks a lot!

cu Martin
--
dont.wanna.tell
[ot]coder - hehe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]