• 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: "John C. Randolph" <email@hidden>
  • Date: Fri, 19 Oct 2001 00:07:09 -0700

On Thursday, October 18, 2001, at 11:38 PM, Todd Gureckis wrote:

hello,

sorry if this is a very basic question, but i can't seem to find an clear answer to this anywhere online.

I am creating an class that needs quite a few internal methods. These methods need not be available to users of the class however. I would however like these methods to share the same variable space as standard interface methods. something like private member functions in c++.

i feel that his must be possible, but i am not sure of the syntax for obj-c.

Typically, this is done in ObjC with a category, whose interface is given within the .m file. (Or, in a .h file that you don't distribute.)

Suppose you're defining MyClass:

in MyClass.h:

@interface MyClass : NSObject
{
}

- publicMethod;

@end

Then, in MyClass.m:

#import MyClass.h

@interface MyClass (secretMethods)

- privateMethod;

@end

@implementation MyClass (secretMethods)

- privateMethod
{
NSLog(@"Hush!")
return self;
}

@end

@implementation MyClass

- publicMethod
{
[self privateMethod];
return self;
}

@end

if the above is unclear: basically I want to know where i can put the method declarations and where to put the method definitions for functions that is are in the public interface but still has access to the private variables of the class.

All methods of the class have access to private instance variables of the class. You can limit what's visible to subclasses with the @private keyword, but most people don't bother.

then my next question is how to call such a function from within a public method.

Same way you call it in any context: [receiver message].

i don't know how obj-c would handle this but, it seems that placing the message

[self myPrivateMethod];

in the middle of a public method like - (void)init would violate the "privateness" of the myPrivateMethod.(??)

Not at all.

-jcr

"I fear all we have done is to awaken a sleeping giant and fill him with a terrible resolve." -Admiral Isoroku Yamamoto, Dec 7, 1941.


References: 
 >private methods? (From: Todd Gureckis <email@hidden>)

  • Prev by Date: Re: Pointers to methods (or functions, for that matter)
  • Next by Date: Re: private methods?
  • Previous by thread: private methods?
  • Next by thread: Re: private methods?
  • Index(es):
    • Date
    • Thread