The Importance of nil
Wednesday, January 14th, 2009Take a look at these two nearly-identical pieces of code. Can you spot the difference?
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@”Choose Image Source”
delegate:self
cancelButtonTitle:@”Cancel”
destructiveButtonTitle:nil
otherButtonTitles:@”Camera”, @”Photo Library”];
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@”Choose Image Source”
delegate:self
cancelButtonTitle:@”Cancel”
destructiveButtonTitle:nil
otherButtonTitles:@”Camera”, @”Photo Library”, nil];
The first one will cause you untold amounts of misery as you futilely search with increasing desperation as the hours go by for the mysterious cause of your app’s sudden propensity to hang when a certain button is pressed. The second one has a “nil” at the end.
(The moral of the story is that variable length arguments need to be terminated with “nil”. Also, don’t program while sleepy.)