Archive for December, 2008

Better IB outlets in Objective-C 2.0


2008
12.15

Apple docs lack clarity when it comes to whether IBOutlets retain objects they point to. However a new way to specify outlets in Obj-C 2.0 saves the day.

@interface MyController : NSObject
{
    NSTextField* _text;
}
@property (readwrite, retain) IBOutlet NSTextField* text;
@end

@implementation MyController
    @synthesize text=_text;
@end

This mechanism allows a developer to control retention behavior association with an outlet. Why should you care? The story with loading a nib (xib) file is that top-level objects are returned retained and it is left up to a developer to release them when they are no longer needed. Misunderstanding of this detail results in numerous leaks across applications.

A way to avoid tracking the top-level objects is to structure nibs so that every top-level object is reachable from the “File’s Owner” and to release top-level objects upon loading. However, this strategy brings about sad results unless one ensures that IBOutlets are always retained.

A nifty Silicon Valley style interview question


2008
12.08

It goes like this… Please write a function that converts a string containing a positive integer into an int. Basically write an atoi function. There are a couple of restrictions: you must use only one loop and can’t use any data structures other than an input array.

Here is a sample implementation

int atoi(char * str)
{    
    int incr = 1;
    int res = 0;

    int p=1;
    for (int j=0; j>=0; j+=incr)
    {
        if (str[j]=='\0')
        {            
            incr = -1;
        }
       
        if (incr < 0 && str[j] != '\0')
        {
            res += p * (str[j]-'0');
            p*=10;
        }
    }

    return res;
}

A case of the blues


2008
12.03

Insipid doom encased me now
Insipid rock of disbelief
When even rhyming
Offers not much of the usual relief

Bereft of art … I robot do
Feed cryptic yarns to  iron loom
Nocturnal runs through ocean smog
Resolve no splinters that consume

When long for wings that mighty once
Did carry me to distant shores
I crave old blood that boiled fast
When freely cordially flows