Here are a few useful cocoa & cocoa touch macros that I’ve come across and thought I share here. Some of those I use regularly and some I just know about but you may find them useful.
Hex (HTML) color to UIColor macro
I found this macro a long time ago somewhere and I’m not sure what’s the original source, yet the Google search returned this blog so I’m going to give him the credit.
#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0 green:((c>>16)&0xFF)/255.0 blue:((c>>8)&0xFF)/255.0 \ alpha:((c)&0xFF)/255.0] //usage UIColor* color = HEXCOLOR(0xff00ff00); |
Enumerating collections macros
The following macro helps you with fast enumeration over collections, credit goes to Wincent.
#define WOEnumerate(collection, object) \ for (id enumerator = [collection objectEnumerator], \ selector = (id)@selector(nextObject), \ method = (id)[enumerator methodForSelector:(SEL)selector], \ object = enumerator ? ((IMP)method)(enumerator, (SEL)selector) : nil; \ object != nil; \ object = ((IMP)method)(enumerator, (SEL)selector)) #define WOKeyEnumerate(collection, object) \ for (id enumerator = [collection keyEnumerator], \ selector = (id)@selector(nextObject), \ method = (id)[enumerator methodForSelector:(SEL)selector], \ object = enumerator ? ((IMP)method)(enumerator, (SEL)selector) : nil; \ object != nil; \ object = ((IMP)method)(enumerator, (SEL)selector)) |
Logging macros
CMLog - use to extend NSLog functionality.
#define CMLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]); //usage CMLog(@"My iPhone is an %@, v %@", [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion]); |
MARK - method stamp macro, use to output class and method name.
#define MARK CMLog(@"%s", __PRETTY_FUNCTION__); |
Benchmarking macros
Use START_TIMER and END_TIMER to get the timing between these two calls.
#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate]; #define END_TIMER(msg) NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; CMLog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]); //usage example - (NSData *)loadDataFromURL:(NSString *)dataURL { START_TIMER; NSData *data = [self doSomeStuff:dataURL]; END_TIMER(@"loadDataFromURL"); return data; } |
Logging and benchmarking macros, courtesy of Stephan Burlot.
UIView frame (CGRect) macros
And for the end, here’s my own modest addition to the list. When developing an iPhone or iPad apps, you probably found yourself lots of times working with UIView frames, changing them etc… Often times I just change the UIView size or I just need to move it around the screen, reposition the view and each time I need to do CGRectMake etc… The following macros have helped me and saved me some typing.
#define width(a) a.frame.size.width #define height(a) a.frame.size.height #define top(a) a.frame.origin.y #define left(a) a.frame.origin.x #define FrameReposition(a,x,y) a.frame = CGRectMake(x, y, width(a), height(a)) #define FrameResize(a,w,h) a.frame = CGRectMake(left(a), top(a), w, h) |
Obviously you can do the same for UIView ‘bounds’ instead of ‘frame’.
Pingback: September roundup on Dizzey.com | Misc
Pingback: [cocoa myNotes]; » Blog Archive » Macros
I’m not entirely sure what the enumeration macro helps with. It seems a lot more complicated than a regular NSEnumeration while loop and even then you’re better off just using fast enumeration:
for (id object in collection) {
}