Re: private methods and variables
Re: private methods and variables
- Subject: Re: private methods and variables
- From: Peter N Lewis <email@hidden>
- Date: Wed, 30 Jul 2008 10:33:30 +0800
At 11:02 PM +0200 29/7/08, Torsten Curdt wrote:
Especially for a framework I don't want to expose implementation
details to the interface.
Others have pointed out that you cannot add ivars in the implementation.
However, if you can control the creation of your objects via a
factory function or other means, then you can have a private subclass
that has ivars and never actually create the published class per se.
For example (typed in email)
.h file:
@interface MyClass
-(void) publicMethod;
+(MyClass*) allocate;
@end
.m file
@interface MyRealClass : MyClass
{
int privateVariable;
}
-(void) privateMethod;
@end
@implementation MyClass
-(void) publicMethod { }
+(MyClass*) allocate
{
return [MyRealClass alloc];
}
@end
@implementation MyRealClass
-(void) privateMethod { }
@end
I've used this technique a fair amount in C++, but, being a novice,
not much yet in Objective C, but it should work. I'm not sure
whether it would be a good idea to for the method named allocate to
be named alloc or not. Possibly that would mean it would work even
if you couldn't control the creation of the class, I'm not sure.
One downside is that Interface Builder does not seem to read .m files
(even if you actively drag them to it), so you can't have any
IBOutlets in MyRealClass unless you put it in a (potentially private)
header file.
Hopefully this makes as much sense in Objective C as it does in C++,
otherwise I'm sure someone will correct me.
Enjoy,
Peter.
--
Keyboard Maestro 3 Now Available!
Now With Status Menu triggers!
Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
<http://www.stairways.com/> <http://download.stairways.com/>
_______________________________________________
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