Re: Objective-C & Private functions
Re: Objective-C & Private functions
- Subject: Re: Objective-C & Private functions
- From: Jeff LaMarche <email@hidden>
- Date: Sun, 8 Feb 2004 16:28:54 -0500
On Feb 8, 2004, at 1:59 PM, Peter Fischer wrote:
Is there any way to create private "Helper" functions in
objective-C? I have not been able to find any information on function
scoping within a .h file. Obviously, a function can be declared &
implemented in the .m file, making it somewhat private. However, the
functions pretty much need to be implemented at the top of the .m file
so that other functions can see them. Is there a way to declare
private functions in the .h file, or at the very least, forward
declare them at the top of a .m file, and provide implementation
further down in the .m. Thanks for any help in advance.
Objective-C doesn't have the ability to declare methods as public,
private, or protected - they're all public. You can declare variables
as protected, public or private, although I honestly haven't really
seen that done much. Cocoa is a much more trusting language than Java
and C++, which are rather paranoid languages. =)
There are a couple of ways of doing the equivalent. One of them is what
you've mentioned - simply not including the method in your header file.
Another way that Apple's engineers seem to use a lot is to declare
categories specifically for blocks of related "private" functionality.
The category can be contained in a separate header and, thus, the
methods won't be obvious to outside coders. If someone does a little
reverse engineering and discovers your method, there's nothing
preventing them from calling it, but any decent Cocoa developer is
going to know that they are using a "private" undocumented method at
their own risk.
Basically, Obj-C will let you call any method (actually "send any
message" is technically what you're doing) on any object without error
(though it won't do anything if the method doesn't exist) - this
ability is inherent in the basic underlying design of Cocoa. By
declaring your methods in a category, or by not including them in your
header file, you are saying "Beware, proceed at your own risk." Don't
worry about totally locking people out the way you do in Java. After
spending some time in Obj-C, you'll come to realize that there's far
less need to do that than you once thought (I know... I asked this
exact same question about 3 years ago on this list). Objective-C's
syntax can be learned in a day, but it takes a little longer to really
wrap your head around it.
Jeff
_______________________________________________
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.