My Favorite Macros for Objective-C Development in Xcode
Some developers say that macros should be avoided, but I find them very useful to add to my Xcode projects.
By simply importing a single file -- UAMacros.h
-- I immediately get the benefits of using these macro shortcuts to make my code much more readable.
I'll admit that a negative point to using this approach is that sharing code without the macro file is not possible.
Usually, if a Macro is used exclusively in one file, I will include it in the .h
file only.
These macros I am showing you below are usually pretty useful across your entire codebase.
Timer Invalidation
This is pretty self explanatory... it simply invalidates and nils the timer in one line. you call it like UA_invalidateTimer(self.timer);
Device Info
These macros allow me to shorten the if statement and make it more readable for code blocks that are device dependent: if (UA_isIPhone) ...
Same as above, these can be used in if
blocks to make it more readable. You can extrapolate and add any UA_isXXXXXXSupported
by creating a new line based on the format of
- Does x respond to the selector?
- Call selector
Threading
This is an interesting one. Many completion handlers for network callbacks and other long running processes are run on background threads. For the most part, when I see examples of this kind of callback being referenced online, the poster envelopes the entire callback on the main thread. Because only UI updates need to be run on the main thread, this can be problematic, especially if you are doing data work before your UI updates. If I have a situation like this, I let the callback run on the background thread, but place this macro as the first line in my UI updating methods. It simply checks to see if the current thread is main, and if it isn't, dispatch the method on the current thread. Caveat: As written, it only works on methods without arguments.
You will probably get a warning when you use this:
To get rid of it, surround the #define
line like so:
Colors
These macros make it easy and shorter to specify colors using RGB values: UA_rgb(255,242,235)
Convenience
There aren't many times when you should have to access the delegate from your classes, but when you do, it should be short and readable: UA_appDelegate.window
Debugging / Logging
UA_log()
is used exactly like NSLog
but shows additional info like the name of the file and the line number.
It is a basic example of a logger that prints to the console log on debug builds and does nothing on the production builds.
Writing to a log, especially if you are a verbose logger, will slow down you production app.
You will also only get a chance to view thees logs on rare occasion, so there isn't much need for them.
Make sure you have a PREPROCESSOR MACRO
set as DEBUG=1
for all non production target build modes.
UA_logBounds
and UA_logFrame
can be used to quickly print the bounds and frame for any view to the console.
NSStringFromBool
is useful for simple logging and should be used along the lines of NSStringFromCGPoint
and NSStringFromCGRect
.
UA_showDebugBorderForView
is something I place in every code-created view.
It allows me to set one BOOL
(UA_SHOW_VIEW_BORDERS
) and instantly get a border printed around the view's layer.
This allows me to ensure that Auto-Layout and positioning is correct as I have intended it to be, and is especially useful on views with transparent backgrounds.
You just have to make sure that UA_SHOW_VIEW_BORDERS
is set to NO
before deploying on the app store.
I use many more macros than these throughout my projects and tend to add macros with these patterns whenever I find a long line that is being repeated frequently, or when a simple logging or debugging method should be used in many places, yet should be enabled with only a single boolean value. Enjoy.
Was this page helpful for you? Buy me a slice of 🍕 to say thanks!
Comments