Objective-C & Private functions

  • Hello -
        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.

      --Pete

    ---------------------------------------------------
    Peter S. Fischer
    http://macosx.syr.edu/spottedsoftware
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On 8/2/04 6:59 pm, Peter Fischer <pfischer...> wrote:

    > Hello -
    > 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.

    Just declare them as static. Static functions follow the same scoping rules
    in Objective-C as in ANSI/ISO C.

    Cheers,

    Chris
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On 2004/02/08, at 13:59, Peter Fischer wrote:

    > Hello -
    > 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.

    You can forward declare the functions in the .m file exactly as you
    would in the .h file, there is no difference in syntax. Also, if the
    function is only to be called from that specific file, it's a good idea
    to declare it static, like so:

    static void foo(blah);

    --
    Clark S. Cox III
    <clarkcox3...>
    http://homepage.mac.com/clarkcox3/
    http://homepage.mac.com/clarkcox3/blog/B1196589870/index.html

    [demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • @interface SomeClass : NSObject (Private)

    -(void)coolFunction1;
    -(void)coolFunction2;

    @end

    Forgive any syntax misses here since I'm at work and cannot verify what
    I'm typing. But it should give you the idea.

    > Hello -
    > 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.
    >
    > --Pete
    >
    > ---------------------------------------------------
    > Peter S. Fischer
    > http://macosx.syr.edu/spottedsoftware
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • 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 | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • 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 <cmh...>
    bDistributed.com, Inc.
    Outsourcing Vendor Evaluation
    Custom Mac OS X Development
    Cocoa Developer Training
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Feb 8, 2004, at 1:59 PM, Peter Fischer wrote:

    > Hello -
    > 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.
    >
    > --Pete

    Assuming you want an Objective C method that's private, and not a C
    function, the way people usually do this is to use a Category:

    In file Foo.m:

    @interface Foo (PrivateMethods)
    - (void) doSomethingPrivate;
    @end

    @implementation Foo

    - (void) somethingPublic {
    [self doSomethingPrivate];
    //... etc
    }

    You can use categories for a bunch of stuff

    * amazingly enough, Categorizing your methods, which makes them easier
    to find in the IDE

    * adding methods to other classes, even those you don't have the source
    to

    * simulating things like private methods

    They're something you don't see in Java and other similar languages so
    they're definitely worth taking a few moments to understand.

    AndyT (lordpixel - the cat who walks through walls)
    A little bigger on the inside

            (see you later space cowboy ...)
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month february 2004 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
Go to today
MindNode
MindNode offered a free license !