Seamus Campbell Seamus Campbell - 1 year ago 67
Objective-C Question

Is there a generic method to test for an #imported file in Objective-C

I'm working on an extension of Core Data functionality. I've got a block of code where I'd like to test if a user's

implements the templated
accessor. But I don't want to require the AppKit framework or
(I might use the functionality in command-line applications), so I'd like to wrap the block in an

Looking at NSApplication.h, there are
s for NSAppKit versions (e.g.
). I could test for an arbitrary one of those, but that doesn't feel quite right. Is there a generic way in the preprocessor to test whether the current compilation environment includes a framework or specific header?

Answer Source

No, there is not, because:

  1. The C preprocessor does not keep track of the files it has included — it just includes them and moves on
  2. The preprocessor has no concept of "frameworks," per se, and they aren't even brought in until the linking stage anyway

The test for an AppKit version is the idiomatic way to do this sort of thing in the preprocessor.

However, i don't see why you need the preprocessor for this.

BOOL delegateImplementsAccessor = [[[NSClassFromString(@"NSApplication") sharedApplication] delegate] respondsToSelector:@selector(managedObjectContext)];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download