• 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, Protected methods in Obj C?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Private, Protected methods in Obj C?


  • Subject: Re: Private, Protected methods in Obj C?
  • From: Andy Lee <email@hidden>
  • Date: Wed, 3 Apr 2002 13:30:40 -0500

At 8:55 AM -0800 4/3/02, Shawn Erickson wrote:
All methods are public. If you want to hide methods you can declare categories within the implementation file. This doesn't mean they can't be accessed, though.
[...]
It is my understanding that private methods shouldn't be listed in your interface definition (@interface - @end block).

A class's methods can be declared in multiple @interface blocks. In addition to the main @interface section that you put in MyClass.h, you can declare groups of methods called categories. The suggestion above is to put a category in the class's *implementation* file (MyClass.m, not MyClass.h) that declares methods you want to treat as private, even though technically, they aren't.

The reason for declaring the private methods at all is so the compiler knows about them, and won't generate a message-not-understood warning or a type-mismatch error when you use them in your implementation code. It's like a forward declaration in C.

The reason for not declaring private methods in MyClass.h, which other classes might import, is so that the compiler *will* generate a warning and/or error if you call a "private" method outside of MyClass.m. Plus a human being reading a .h file will only see the methods you really wanted them to know about.

Basically the interface definition for a class contains all externally usable methods (lists the message the class supports).

Not to be nit-picky, but not quite. First, *all* of a class's methods are externally usable, whether they are listed in an interface or not. You can partially hide a method declaration from the compiler, but you can't hide the method itself at runtime.

Second, a class's methods are not necessarily declared in one single @interface block. Besides categories, as I mentioned above, there are also @protocol declarations, which can be shared by multiple classes. The issue is where you *put* a given @interface block: whether in a .h that might be imported by other classes, or in a .m, to hide certain method declarations from the compiler when it compiles other files.

--Andy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Private, Protected methods in Obj C? (From: Shawn Erickson <email@hidden>)

  • Prev by Date: Re: Drawing a Grid in a TableView
  • Next by Date: Re: Dumb First Time Apple Scripting question
  • Previous by thread: Re: Private, Protected methods in Obj C?
  • Next by thread: Re: Private, Protected methods in Obj C?
  • Index(es):
    • Date
    • Thread