Re: private methods and variables
Re: private methods and variables
- Subject: Re: private methods and variables
- From: Torsten Curdt <email@hidden>
- Date: Wed, 30 Jul 2008 04:45:27 +0200
Thanks for that!
Really interesting - but also sounds really terrible :)
Makes you wonder why stuff like hasn't been fixed in Objective-C 2.0.
cheers
--
Torsten
On Jul 30, 2008, at 04:03, Ken Thomases wrote:
On Jul 29, 2008, at 8:22 PM, Torsten Curdt wrote:
So how can I have private ivars that don't show up in the interface?
You can use the pImpl (pointer-to-implementation) idiom for classes
that you're writing yourself:
@class MyPrivateStuff;
@interface MyClass : NSObject
{
MyPrivateStuff* pImpl;
}
/* ... */
@end
Then, in your .m file:
@interface MyPrivateStuff : NSObject
{
id foo;
NSString* bar;
/* ... */
};
@end
@implementation MyPrivateStuff
/* ... */
@end
@implementation MyClass
- (id) init
{
if (self = [super init])
{
pImpl = [[MyPrivateStuff alloc] init];
/* ... */
}
}
@end
For classes which you are not writing yourself, you can still extend
them with categories. For properties, you add the usual accessor
methods, which is straightforward with a category. Providing per-
instance backing storage for the properties is more complicated.
One technique is to use a "category variable" (like the "class
variable" we were discussing earlier; really just a static file-
scope variable) to keep an associative collection which maps from
"self" to a mutable dictionary of property values. NSMapTable is a
good candidate for this sort of collection.
Cheers,
Ken
_______________________________________________
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