Showing posts from November, 2013

A better way to do NSLogging

There is much better way to do logging in your iOS apps than using NSLog directly. The problem with that is that you have to go to every NSLog statement you have in your entire codebase whenever you want to make a change to them or want them out of your current build, etc. etc. You would always want to remove your NSLog's before doing a release build and this could be an extremely tedious task. Also, many people don't know that NSLog's can be very bad for performance. So whenever you would want to test whether logging is causing your performance issues and you just quickly want to remove them, you would also want a quick way to just switch logging off. I came across this macro in a project that I was working on. It is just awesome. #ifdef DEBUG #   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else #   define DLog(...) #endif Using this DLog macro your logs will automatically include the method

Bitwise operators for options

There's a very useful technique that is available to developers that hardly anyone knows about these days. Bitwise operators! Basically what you are doing is manipulating the individual bits within a variable. Personally where I have found this extremely useful is for having a bunch of options and passing them around in a single integer variable. For example, let's look at the orientation options of a mobile application: Portrait for the first bit, LandscapeRight for the second bit, LandscapeLeft for the third bit, PortraitUpsideDown for the fourth bit We can represent an application that can only be in portrait mode, regardless if the device is upside down with the following: 1001 which equals an integer value of nine (9) or the sum of one (for the first bit) and eight (for the fourth bit) To get this you could do the following: int options; options += 1 << 0;   //Portrait                      // 0001    // integer value of 1 options += 1 << 3;

Singletons in Obj-C

Came across this pattern the other day: static dispatch_once_t onceToken; dispatch_once (&onceToken, ^{ // Do some work that happens once }); This is the better way to implement a singleton in Obj-C. Reduces the code needed from the normal way of coding it out yourself, and guarantees that your object will only be instantiated once. Some nice reading on the matter at  bignerdranch  and at  stackoverflow