4

Can someone confirm that you cannot access instance variables defined in an Objective C @implementation block from within C style functions of the same class? The compiler is throwing errors saying "XXX undeclared' where XXX is the instance variable name.

Here's an example of what I am explaining:

   @interface FontManager : NSObject {      
                CGFontRef fontRef;
   }

   static int CstyleFunction() {
        NSUInteger emSize = CGFontGetUnitsPerEm(fontRef);
   }

I want to verify that I cannot use "fontRef" from withing "CstyleFunction".

Any insight would be greatly appreciated.

3 Answers 3

3

A "C style method" doesn't really deserve the name "method", I'd call it a "function" instead as in C.

A C function has no self, so it cannot implicitly access ivars as a method can. If you pass an instance to the C function as a parameter, you can access ivars in the same manner you would access a field in a struct pointer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the first response. I would consider this a great answer, but @skue's post below provides two options for resolving the issue. Also thank you for the correction on my semantics.
1

@Anomie and @jlehr are correct, the C function has no concept of the FontManager object and its current state, it just happens to live in the same file.

However, if FontManager is a singleton and you make fontRef a property (or create an accessor for it), then it would be possible to access the value within your C class:

static int CstyleMethod() {
    FontManager *fm = [FontManager sharedManager];
    NSUInteger emSize = CGFontGetUnitsPerEm(fm.fontRef);
}

Bottom line, you can mix-and-match C and ObjC syntax within C functions & ObjC methods. But because C functions have no default reference to self (and the object's associated instance variables), you can only reference ObjC objects that are singletons, stored in a global variable, or passed in as parameters.

1 Comment

Thanks for the response. I had done some more research about this topic and am understanding this concept. I have since implemented the use of a static C variable that holds a reference to an instance of my class. I use this to call my instance property methods in order to access the instance variables from within C functions (and not methods as others have wisely corrected me on).
1

That's correct. You seem to be mixing up methods and functions though. Methods exist only in Objective-C. What you're referring to as a 'C style method' is really just a C function.

C is not an object-oriented programming language. Since there's no such thing as an object in C, there's also no such thing as an instance variable in C, so the fontRef instance variable would not be visible in the function you posted, nor for that matter in any other C function in your program.

1 Comment

Good point. I've seen this argument that C functions should not be called methods in other posts, and it is a valid one. Semantics are important when trying to understand concepts. I appreciate the feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.