Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for the existence of a Class when I can't weak link against it

I am writing a class that is designed to be re-usable but I have included calls to a class which may not always be available when compiled.

To be more specific I am using Flurry analytics and I want to included calls to that - but not every project that this module may be included in will have access to the Flurry library - I simply want it not compile in those bits of code in these situations. The module will be distributed as source code, so it only has to test at compile time.

So far I have tried:

if([Flurry class]){
    [Flurry logEvent:@"Blah"];
}

This fails "Use of undefined identifier" - Flurry is not defined anywhere, as there is no weak reference to the library

So next tried:

Class flurryClass = NSClassFromString(@"Flurry");
if(flurryClass){
  [flurryClass logEvent:@"Blah"];
}

This fails as "No known class method for selector logEvent"

So I seem to be stuck, as the first method relies on weak linking a library that may not be available to weak link against! The second method I assume fails as I'm calling a Class method on a Class that is currently has no definition so the compiler complains? Is there any solution to this that anyone can think of?

like image 266
SimonB Avatar asked Dec 15 '22 13:12

SimonB


1 Answers

You have to use reflection the whole way through. Change your code to this:

Class flurryClass = NSClassFromString(@"Flurry");
if(flurryClass){
  [flurryClass performSelector:@selector(logEvent:) withObject:@"Blah"];
}
like image 182
Chris C Avatar answered Mar 09 '23 01:03

Chris C