• 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: Jonathan Hess <email@hidden>
  • Date: Sat, 23 Feb 2008 14:15:28 -0800


On Feb 22, 2008, at 6:08 AM, Adam P Jenkins wrote:

On Feb 22, 2008, at 6:48 AM, Rob Petrovec wrote:

Lack of private methods is a serious flaw in Obj-C IMO.

This is silly. Private is really just about as voluntary in C++ as it is in ObjC. If a user of your class wants to ignore your decisions about what should be private methods, they have only to write:


#define private public
#include "yourclass.h"
#undef private

void foo() {
 YourClass obj;
 obj.aPrivateMethod();  // no compiler errors or warnings
}

So declaring things private in C++ is just an advisory to users of your class that certain methods and fields aren't intended to be used by clients of the class, it's not meant as any kind of security mechanism. In ObjC you can achieve the same thing by not declaring methods in the interface file, so if a user of your class still finds out about the method somehow and calls it, they'll get a compiler warning and will know they're doing something not intended by the class's author.

Note that this is in contrast to Java or C#, which run in managed environments, and where private really can be used to fairly securely block certain methods and fields from being accessed, when using the correct security settings in the VM.

That said I do agree that it's nicer to have a formal language construct for specifying what's private, like in C++, than the ad- hoc mechanisms available in ObjC. I just don't agree that it's really a serious flaw, more of a minor flaw._______________________________________________

If a method is private, why would you ever want to place it in a public header? I could see a justification for @proected applying to methods, but it seems much better to just not have the private methods in the interface at all. Putting a private method in the interface says "This exists, and you can't use it." If I can't use it, why are you telling me it is there?


I believe that in c++ the MyClass.h file is the *only* interface to your class. In objective-c, the MyClass.h file is simply the public interface to your class. You're free to have more than one @interface and you are encouraged to use them to declare things that shouldn't be part of your outward facing public interface.

Things would be best if we didn't even have to put our private instance variables in the header -
Jon Hess




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

_______________________________________________

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


  • Follow-Ups:
    • Re: Private Methods
      • From: Adam P Jenkins <email@hidden>
References: 
 >Private Methods (From: Philip Bridson <email@hidden>)
 >Re: Private Methods (From: Mike Abdullah <email@hidden>)
 >Re: Private Methods (From: Rob Petrovec <email@hidden>)
 >Re: Private Methods (From: Adam P Jenkins <email@hidden>)

  • Prev by Date: bind tableview to arraycontroller
  • Next by Date: Re: NSOutlineView autosaving of expanded/collapsed state
  • Previous by thread: Re: Private Methods
  • Next by thread: Re: Private Methods
  • Index(es):
    • Date
    • Thread