March 2010
6 posts
Choosing the right spot for your task
Usually life with Cocoa is nice. You have hooks and handles for every occasion. However - sometimes you are not looking in the right place, or have no idea this hook you need exists at all. A strange and interesting example from SubEthaEdit is the [NSAttributedString doubleClickAtIndex:] method. I had to ask for that at WWDC because I was sure this kind of functionality would either be named...
Mar 29th
Handy constants
Another element that is easily overlooked, but can make code and life so much easier: Convenience Constants. Take some examples: CGPointZero CGRectZero CGSizeZero CGRectInfinite - mostly used in Core Image contexts CGRectNull - not equal to CGRectZero, returned by e.g. CGRectIntersection() NSNotFound CGAffineTransformIdentity UIEdgeInsetsZero UITrackingRunLoopMode ...
Mar 26th
Cocoa is object oriented, right?
Of course it is. And greatly designed too if you ask me. However there is a huge world of regular functions and macros which you might have overlooked. Here are some of my favorites: UIImagePNGRepresentation(image) UIImageJPEGRepresentation(image,quality) UIImageWriteToSavedPhotosAlbum(image,target,selector,context) UIInterfaceOrientationIsLandscape(orientation) ...
Mar 25th
Occam's Razor
In debugging a Problem always, I repeat, always apply Occam’s Razor and look for the simplest solution out there. 9 out of 10 times it is the blatant obvious errors you are just attributing to some higher power, deeper edge case or otherwise dark wizardry. Our brains are wired this way, we see connections where there are none, we associate. I’ve seen it time over time, and experienced...
Mar 24th
+initialize
When implementing a Cocoa Classes +initialize method, don’t forget to check if you are you, otherwise you might get called twice! @implementation MyCustomClass +(void)initialize {  if (self == [MyCustomClass class]) {    // do your stuff here!   } }
Mar 17th
Retain, Release, Autorelease, etc.
Something I tend to do, which saved my but quite some time. Interestingly enough this is somewhat discouraged in iPhone OS due to the overhead of autoreleases and the relatively small power of the platform. Instead of: SomeClass *myInstance = [[SomeClass alloc] init]; // do something with myInstance [myInstance release]; // release it again I do: SomeClass *myInstance = [[[SomeClass alloc]...
Mar 10th