Functions, classes and ivars

Discussion in 'Mac Programming' started by jivaro, Jun 9, 2012.

  1. jivaro macrumors newbie

    Apr 21, 2012
    Cornella de Llobregat (Spain)
    How can I define an Objective C function in the @implementation of a class in such a way that the function has access to the ivars of the class ?

    Thanks for any hint/help

  2. pstoehr macrumors member

    Aug 5, 2003
    Scheßlitz, Oberfranken, Bavaria, Germany
  3. Sydde macrumors 68020


    Aug 17, 2009
    Traditional C functions will not have ivar access, but you might want to check out Blocks. More stuff to learn, but very useful down the road.
  4. jivaro thread starter macrumors newbie

    Apr 21, 2012
    Cornella de Llobregat (Spain)

    My problem is not related to class methods, but to objective-C functions defined in the class. These functions are intended to be called by several of the methods of the class, but there is no way that they have access to the ivars of the class (independently that the function be defined inside the @implementation or outside it). Xcode compiler gives me an error and says that the corresponding IBoutlets are not defined when using them in the function.

    I must say that I'm quite novice to Cocoa and I do not know if I'm missing something obvious...

  5. chown33, Jun 9, 2012
    Last edited: Jun 9, 2012

    chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    There is no such thing as "objective-C functions defined in the class". Their position in the class is incidental. They're exactly the same as Objective-C functions defined outside any class. And Objective-C functions are exactly the same as C functions.

    Post a compilable example of one of your functions that causes a problem. Be sure to post the @interface and @implementation for the class, too.

    C functions are C functions. They are not class methods or object methods. Only methods have an automatic self reference, and implicit access to ivars (object methods only). Everything else, including all C functions no matter where they are defined, does not.

    In a C function, or in any method, this notation:
    objectPtr -> ivar
    is the way to reference an accessible ivar. In short, an object pointer can be used as a pseudo-struct pointer, where the pseudo-struct comprises the object's accessible ivars.

    To write a C function that uses an object pointer to refer to an ivar (pseudo-struct member variable), you must write the C function with an explicit object-pointer parameter, exactly as if you were passing a real struct pointer to a function.

    Only accessible ivars are accessible this way (@public or @protected). If in the @implementation of the class, then @private should also work (I think).

    This is a feature of Objective-C, the language. It has nothing to do with Cocoa, the library.

    See this reference doc:
    Heading: Defining a Class > Class Implementation > Referring to Instance Variables

    Also see Wikipedia's Objective-C article:

    Find the 2nd use of -> on the page.

    You might also benefit from this:
    It's not for the inexperienced, but it explains the mechanics of how Objective-C makes methods into C functions and how it calls them.
  6. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    1. A class doesn't have ivars. Objects of a class have ivars.

    2. A C function needs to be passed a reference to the particular object for which you want access to an ivar (a method automatically gets it as a hidden parameter). Given an object pointer, a C function in an Objective C implementation file can message the getters and setters for the desired ivar within that object.

    3. There is a difference between C functions. A C function in a .c file can not message an object using Objective C syntax. A C function in an Objective C implementation scope can.
  7. Sydde, Jun 10, 2012
    Last edited: Jun 10, 2012

    Sydde macrumors 68020


    Aug 17, 2009
    Not true. In fact, I have done it. If your .c file or its header import/include the framework(s) it needs, it can easily use object messaging.


    Functions define as blocks have access to ivars. Read Apple's Introduction to Blocks (OS X 10.6 and later).

Share This Page