September 24th, 2008

Objective fun

This would make an excellent recruitment-test for an Objective-C programmer.
Both of these snippets are from the Apple developer example code collection. One of them is correct, and the other will likely make your application burst into flames*.

Code snippet 1:

- (void)setArguments:(NSDictionary *)arguments
{
    [arguments copy];
    [_arguments release];
    _arguments = arguments;
}

Code snippet 2:

- (void)setURLToLoad:(NSURL *)URL
{
    [URL retain];
    [URLToLoad release];
    URLToLoad = URL;
}

Quiz questions (select text for answer):
Q: Which one is broken? (1p)
A: Snippet 1. It throws away the copy, and instead assigns a non-retained variable.
Q: Why is the other one not broken? (2p)
A: Because retain modifies the object, so assigning the retained object is fine.
Q: Why is the author using copy instead of retain? (5p)
A: Because the object could be a NSMutableDictionary which could unexpectedly change later.
Q: How should they both be written instead so that this will never happen? (20p)
A: Like ‘[_v release];_v = [v retain]’ or ‘[_v release];_v = [v copy]’

Thanks Apple, made my day here (hey, it’s raining).
*) The computer supplies ‘BusError’ and you’ll have to provide the pyrotechnics. I usually go with illustrative hand-waving and onomatopoetic sound effects.

Posted by Roine at September 24th, 2008 15:09 | Permalink

April 14th, 2008

Songkran

Water splashing festivities started late today. I was up at 6am and the festivities didn’t kick off until ten.
I used the same camera this year too, in a plastic bag. This year I didn’t bother making a glass lens cover as I knew it would just fog up. I managed to snap 300 pictures before the camera permanently fogged up at around 4pm. The splashing had gotten more subdued the last hour so I decided to call it a day. I had been out for nine hours in blistering sun, and despite double layer spf30 sunblock I’m now burnt to a crisp. My arms are so red they’re actually blinking.
I thought I got some nice shots, but checking them later I barely got a few ‘OK’ ones. Many were ruined by fogging but I guess most were ruined by me.

Songkran picture Songkran picture Songkran picture Songkran picture

While at it I also added more albums to both 2006 and 2007 which should have been up long time ago.

Posted by Roine at April 14th, 2008 21:04 | Permalink