Seamus Campbell
Seamus Campbell

Reputation: 17916

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 NSApplicationDelegate implements the templated managedObjectContext accessor. But I don't want to require the AppKit framework or NSApplication (I might use the functionality in command-line applications), so I'd like to wrap the block in an #ifdef.

Looking at NSApplication.h, there are #defines for NSAppKit versions (e.g. NSAppKitVersionNumber10_0). 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?

Upvotes: 0

Views: 125

Answers (1)

Chuck
Chuck

Reputation: 237080

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)];

Upvotes: 1

Related Questions