PDA

View Full Version : Functions, classes and ivars




jivaro
Jun 9, 2012, 04:28 AM
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



pstoehr
Jun 9, 2012, 07:07 AM
Hi Joan,

every Objective-C method has access to the ivars of the class.

Can you please give a full example that describes your problem better.

Best regards
Peter

PS: By the way, have you already read this document? https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005149

Sydde
Jun 9, 2012, 10:56 AM
Traditional C functions will not have ivar access, but you might want to check out Blocks (http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/_index.html). More stuff to learn, but very useful down the road.

jivaro
Jun 9, 2012, 12:20 PM
Hi Joan,

every Objective-C method has access to the ivars of the class.



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

chown33
Jun 9, 2012, 12:59 PM
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.

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:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html%23//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/mac/#documentation/Cocoa/Reference/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.

firewood
Jun 9, 2012, 04:29 PM
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.

Sydde
Jun 10, 2012, 03:26 PM
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.

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.

----------

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

Functions define as blocks have access to ivars. Read Apple's Introduction to Blocks (http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/_index.html) (OS X 10.6 and later).