• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: private methods?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: private methods?
      • From: Norbert Heger <email@hidden>
References: 
 >private methods? (From: Todd Gureckis <email@hidden>)

  • Prev by Date: Re: private methods?
  • Next by Date: Re: private methods?
  • Previous by thread: Re: private methods?
  • Next by thread: Re: private methods?
  • Index(es):
    • Date
    • Thread