Re: private methods?
Re: private methods?
- Subject: Re: private methods?
- From: Ondra Cada <email@hidden>
- Date: Fri, 19 Oct 2001 10:36:34 +0100
Todd,
>
>>>>> Todd Gureckis (TG) wrote at Fri, 19 Oct 2001 01:38:55 -0500:
TG> I am creating an class that needs quite a few internal methods. These
TG> methods need not be available to users of the class however. I would
TG> however like these methods to share the same variable space as standard
TG> interface methods. something like private member functions in c++.
// header file
@interface AClass ...
// no '_aPrivateMethod' here
@end
// implementation file
@implementation AClass
-(void)_aPrivateMethod { ... }
...
-(void)publicMethod {
...
[self _aPrivateMethod];
...
}
@end
In case you happen to have circular references in those private methods, so
that they have to be declared, the solution is
// implementation file
@interface AClass (MyPrivateMethods)
-(void)_aPrivateMethod1;
-(void)_aPrivateMethod2;
@end
@implementation AClass
-(void)_aPrivateMethod1 { ... [self _aPrivateMethod2];... }
-(void)_aPrivateMethod2 { ... [self _aPrivateMethod1];... }
...
@end
Incidentally, those methods are in fact public too -- if any unrelated code
happens to use
...
id o=[AClass new];
[o _aPrivateMethod1];
...
compiler would issue a warning of unknown message, but the method _will_ be
called properly. Due to the dynamic object system it is alas impossible to
have really internal methods. Therefore, you should at least use underscores
like in this example.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc