Search code examples
objective-cautomatic-ref-countingchipmunk

options to forward declare a C typedef in obj-c @interface


My obj-c class uses a C library (the Chipmunk physics engine), which has an Obj-C wrapper interface.

I want to add a property with a chipmunk type (cpLayers) to my object, like so:

@interface
@property cpLayers layers;
...

The easiest way is to #import "ObjectiveChipmunk.h", but that seems ridiculous to import all of the headers just to get one measly type.

If I #import the C "chipmunk_types.h" file where cpLayers is defined "typedef unsigned int cpLayers;", I get compiler errors related to ARC. They are bridge/casting errors in a macro that is defined in chipmunk_types.h and used in my .m file.

If I add just the definition, or #include chipmunk_types.h, I get redefinition errors.

Is there any better way to do this? And WHY the ARC errors?


Solution

  • Take a look at the ObjectiveChipmunk.h, that is where it overrides the basic Chipmunk types using preprocessor defines. You can add those defines as compiler flags if you want to work around the problem, but I wouldn't really worry about it. You are already doing Objective-C programming after all, have you ever looked at the gargantuan amount of includes that get pulled in when you import something as innocuous as Foundation.h? Importing the full ObjectiveChipmunk.h header is like 1% in comparison.