Mac OS X's Preferences System (and More!)
Subject:   tests for nil in awakeFromNib incorrect
Date:   2001-08-31 18:56:33
From:   mikebeam
Response to: tests for nil in awakeFromNib incorrect

Indeed, records wouldn't be nil after doing [NSMutableArray alloc]. Thanks for catching that!

Now, in your fix wouldn't we want to do
if ( [records count] == 0 ) instead of
if ( [records count] == nil )? I'm trying to think if there's a more fundamental to test whether or not the initialization was successful, but can't think of anything at the moment.

The last [self saveData] isn't strictly necessary, as the preferences file would be re-written the first time a record is added. I was actually debating whether or not to do just this when i was writing this code, and I decided to keep things less cluttered, pedagogically speaking. But on the other hand, it's not a bad idea either.

Thinking about it a little more, we could have two different types of if statements, rather than the way we've been doing it. For example, we could do the following:

if ( nil != [prefs arrayForKey:@"Addresses"] ) {
records = [[NSMutableArray alloc] initWithArray:[prefs arrayForKey:@"Addresses"] )
} else if ( [[NSFileManager defaultFileManager] fileExistsAtPath:recordsFile] == YES ) {
records = [[NSMutableArray alloc] initWithContentsOfFile:recordsFile];
} else {
records = [[NSMutableArray alloc] init];

(I hope this code posts right, tough in this small editing window :)

1 to 1 of 1
  1. Michael Beam photo tests for nil in awakeFromNib incorrect
    2001-08-31 19:04:10  Michael Beam | O'Reilly Author [View]

    • tests for nil in awakeFromNib incorrect
      2003-05-18 20:33:11  anonymous2 [View]

1 to 1 of 1