Quick tip: hiding deliberate selector mistakes

iOS   objective-c   macros   tips  

Somethings we want to enter a selector in Objective-C that will trigger an warning/error in the compiler. It might be a selector for a different class, it might be an invalid selector we are using for unit testing. Whatever the reason, there are times when it's valid to want to use an unknown selector, yet as a general project setting, have selector related warnings turned on.

The way to do this is usually to enter some pragmas around the offending selector code like this:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wselector"
#pragma clang diagnostic ignored "-Wundeclared-selector"
SEL aSel = @selector(xxxx);  
#pragma clang diagnostic pop

But recently I added this little macro to my collection which allows me to hide these warnings when I need to without having to type all the boiler plate:

#define ignoreSelectorWarnings(code) \
_Pragma ("clang diagnostic push") \  
_Pragma ("clang diagnostic ignored \"-Wselector\"") \  
_Pragma ("clang diagnostic ignored \"-Wundeclared-selector\"") \  
code \  
_Pragma ("clang diagnostic pop")  

I bet you didn't know there was a way to embed pragmas within pragmas. But there it is. Here's an example of using this macro

ignoreSelectorWarnings(  
SEL aSel = @selector(xxxx);  
)

Enjoy !


Comments powered by Disqus