Re: @properties and Information Hiding
Re: @properties and Information Hiding
- Subject: Re: @properties and Information Hiding
- From: Karl Goiser <email@hidden>
- Date: Fri, 2 Nov 2007 09:57:27 +1100
Thanks for the replies, everybody!
I was afraid that that might be the recommendation!
So, let's make a comparison using class extensions (this seems to me
to be the most applicable because it allows the compiler to do more
checking which is good):
Before:
<interface file>
@interface MyClass : NSObject {
NSString *value;
}
- (NSString *) value;
@end
<implementation file>
@implementation MyClass
- (NSString *) value {return value};
- (void) setValue: (NSString *) stringParameter {
[stringParameter retain];
[value release];
value = stringParameter;
}
@end
After:
<interface file>
@interface MyClass : NSObject {
NSString *value;
}
@property(readonly, retain) NSString *value;
@end
<implementation file>
@interface MyClass ()
@property(readwrite, retain) NSString *value;
@end
@implementation MyClass
@synthesize value;
@end
Is this how I should implement public getters and private setters?
If so, this hardly seems like an advantage. Sure, the number of lines
has decreased, but that is mostly in terms of white space, and only
marginally - and traded off with hiding the implementation of the
actual methods. Also, there were three places where I have to worry
about the instance variable (in the class declaration, getter method
declaration and method implementations), now there are four (in the
class declaration, getter method declaration, class extension and
synthesize statement). which won't make maintenance any easier.
I'm sorry, I don't want to turn this into a language war (and incur
the wrath of the list gods), I just would like to know what's the best
way to implement this pattern in my Cocoa code...
Thanks,
Karl
On 02/11/2007, at 2:00 AM, Raffael Cavallaro wrote:
On Nov 1, 2007, at 9:07 AM, Karl Goiser wrote:
Along these lines, I am looking for advise on how to implement a
pattern that I am sure many people use: having a private setter and
a public getter. Before properties, I'd implement the two methods
in the implementation part and only declare the getter in the
interface part. But now, in the 2.0 world, how can I leverage the
magic of synthesis to implement such a pattern?
p. 50 of the Objective-C 2.0 pdf:
"This enables two common implementation patterns; mutable
subclass of an immutable class (NSString, NSArray, and NSDictionary
are all examples) and a
property that has public API that is readonly but a private
readwrite implementation internal to
^
^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the class."
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden