Re: Objective-C & Private functions
Re: Objective-C & Private functions
- Subject: Re: Objective-C & Private functions
- From: Chris Hanson <email@hidden>
- Date: Sun, 8 Feb 2004 17:08:33 -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.
To create a "private" function that's local to the current compilation
unit, use "static" as in:
static int min(int a, int b) {
return (a < b) ? a : b;
}
If you mean methods rather than functions, then there is generally no
such thing as access control on methods in a true object-oriented
language like Objective-C or Smalltalk. A method is called in response
to a message, and you can send any message to any object at any time.
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.
One workaround is to declare methods you don't want to expose in a
category:
@interface MyClass (BDInternalMethods)
- (void)privateMethodOne;
@end
atop your implementation (.m) file or in a separate header file, and
then define them in a category too:
@implementation MyClass (BDInternalMethods)
- (void)privateMethodOne {
NSLog(@"-[MyClass(BDInternalMethods) privateMethodOne]");
}
@end
Note that the user can run class-dump on a binary and see both the
category and the methods within it, and someone could write code that
sends your object -privateMethodOne as well. But if they don't have or
include your separate header, they may get a compiler warning that
MyClass doesn't respond to -privateMethodOne.
-- Chris
--
Chris Hanson <email@hidden>
bDistributed.com, Inc.
Outsourcing Vendor Evaluation
Custom Mac OS X Development
Cocoa Developer Training
_______________________________________________
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.