more info ⬇

@amattn

subscribe for the best
iOS code & business tips:

iOS, server tech, and the business of software



2014 04 02

Useful Objective C Macros

A short treatise on snippits of indispensable utility.

Four broad categories of macros end up useful enough to find a home in the highly coveted precompiled header file.

  1. Convenience
  2. Logging
  3. Assertions/Sentinels
  4. Utility

For the sake of brevity, just the names of the macros are listed here, but you can find the full source at https://github.com/amattn/AMNGlobalUtilities/blob/master/GlobalUtilities.h

Convenience/Utility Macros

You should have your own set of useful macros that tend to follow you around. These are the one useful enough that I bring them along to new projects.

// Quickly make an NSError
#define AMN_QUICK_ERROR(error_code, error_description) 

// Quickly get portrait mode
#define AMN_ORIENTATION_IS_PORTRAIT 

// Profiling Macros
// Will log total time elapsed as from START_TIME to CHECK_TIME within a method.  The tag must be equal.
#define AMN_START_TIME(tag)
#define AMN_CHECK_TIME(tag)

Log Macros

Debugging is great. But sometimes you need log statements to get a better sense of flow through code. LOG_OBJECT and LOG_METHOD in particular get lots of usage.

These implementations not only log the value of the variable, but the name of the variable as well. LOG_OBJECT(self.id) prints out:

2013-05-08 11:52:45.615 ExampleApp[71311:c07] self.id 3716426951 SomeController.m:91

There exist variations that are far more flexible, but I prefer being explicit about type.

// standard types
#define LOG_BOOL(object)
#define LOG_CHAR(object)
#define LOG_INT32(object)
#define LOG_UINT32(object)
#define LOG_LONG(object)
#define LOG_ULONG(object)
#define LOG_INT64(object)
#define LOG_UINT64(object)
#define LOG_FLOAT(object)
#define LOG_DOUBLE(object)

// platform dependent
#define LOG_INTEGER(object)
#define LOG_UINTEGER(object)

// Various Cocoa/Objective-C log macros
#define LOG_OBJECT(object)
#define LOG_KIND_OF_CLASS(object)
#define LOG_METHOD
#define LOG_END_METHOD
#define LOG_METHOD_NAME
#define LOG_METHOD_THREAD
#define LOG_CLASS_METHOD

// Various Cocoa struct log macros
// NSRange
#define LOG_RANGE(range)
// CGPoint
#define LOG_POINT(point)
// CGSize
#define LOG_SIZE(size)
// CGRect
#define LOG_RECT(rect)
// CLLocationCoordinate2D
#define LOG_COORD2D(coord)

Assertion/Sentinel Macros

Assertion macros are very good for tightening up extremely rare edge cases. You shouldn’t be using assertion as general error handling in Cocoa. In fact, you shouldn’t be using try/catch at all in most Cocoa code; it is not idiomatic. However during development, macros such as ASSERT_IS_CLASS, SHOULD_NEVER_GET_HERE, and ASSERT_NOT_NIL represent a quick, readable way to explicit about intention and also noisily alert during testing.

This particular implementation first prints out an easy to spot header, then follows up with file, line number, object info and an indispensable stack trace.

If you’d like support in the debugger, “Show Breakpoint Navigator”, click the little plus in the bottom left of the window, and then “Add Exception Breakpoint…”. The default values will then cause the debugger to pause upon encountering any assertion.

// Standard Assertions
#define ASSERT_NIL(x)
#define ASSERT_NOT_NIL(x)
#define ASSERT_ALWAYS
#define ASSERT_TRUE(test)
#define ASSERT_FALSE(test)
#define ASSERT_WITH_MESSAGE(x)
#define ASSERT_TRUE_WITH_MESSAGE(test, msg)
#define ASSERT_FALSE_WITH_MESSAGE(test, msg)

// Useful, protective assertion macros
#define ASSERT_IS_CLASS(x, class)
#define SUBCLASSES_MUST_OVERRIDE
#define SHOULD_NEVER_GET_HERE

Utility

Lastly, just a couple of utility functions that are too useful to leave out. This is kind of a junk draw to be honest.

// Unabashedly cribbed from Wil Shipley (of Delicious Monster fame)
// http://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html
static inline BOOL isEmpty(id thing)
// UUIDs are uuuseful.
static inline CFStringRef createUniqueString(void)

Again, you can find the full source at: https://github.com/amattn/AMNGlobalUtilities/blob/master/GlobalUtilities.h



recently on amattn.com:

2014 02 11 〜 Thoughts on API Design

2014 02 04 〜 Better Apps with Clang's Weverything or Wall is a Lie!

2013 10 15 〜 Venture Capital Math 101: Pre-money, Post-money, Seed, Series A, B, C, D, up rounds and down rounds

2013 10 09 〜 PaSH is the New SaaS, or How to Jump the Long Slow Ramp of Death



greatest hits of amattn.com:

2011 12 07 〜 ARC Best Practices

2013 04 22 〜 Weighted Credit Pools for API Rate Limiting

2012 05 29 〜 You Should Foster a Culture of Readability

2011 11 16 〜 @amattn on iOS Prototyping

2011 10 15 〜 The Disposition of Objective-C




the fine print:
aboutarchive@amattn
© matt nunogawa 2010 - 2014
back ⬆