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?
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"];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With