Functions, classes and ivars

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

  1. macrumors newbie

    Joined:
    Apr 21, 2012
    Messages:
    16
    Location:
    Cornella de Llobregat (Spain)
    #1
    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

    Joan
     
  2. macrumors member

    Joined:
    Aug 5, 2003
    Messages:
    94
    Location:
    Scheßlitz, Oberfranken, Bavaria, Germany
    #2
  3. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    Messages:
    1,896
    #3
    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. macrumors newbie

    Joined:
    Apr 21, 2012
    Messages:
    16
    Location:
    Cornella de Llobregat (Spain)
    #4
    Peter,

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

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

    macrumors 603

    Joined:
    Aug 9, 2009
    Messages:
    6,404
    #5
    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:
    Code:
    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:
    http://developer.apple.com/library/....html#//apple_ref/doc/uid/TP30001163-CH12-SW1
    Heading: Defining a Class > Class Implementation > Referring to Instance Variables

    Also see Wikipedia's Objective-C article:
    http://en.wikipedia.org/wiki/Objective-C

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


    You might also benefit from this:
    https://developer.apple.com/library...rence/ObjCRuntimeRef/Reference/reference.html
    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. macrumors 603

    Joined:
    Jul 29, 2003
    Messages:
    6,308
    Location:
    Silicon Valley
    #6
    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

    macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    Messages:
    1,896
    #7
    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