advertisement

Article:
  Mac OS X's Preferences System (and More!)
Subject:   tests for nil in awakeFromNib incorrect
Date:   2001-08-29 19:55:01
From:   jeremym
Hi,


The test "if ( nil == records )" within the awakeFromNib method do not work. You use


records = [[NSMutableArray alloc] initWithArray:[prefs arrayForKey:@"Addresses"]];


To read the data from the preferences system into the data structure. Once the alloc is performed the pointer "records" no longer points to nil, regardless of what happened in the init code. You can test this by pointing a break point in the code, or like I did on accident. Run the program in the final form, add some stuff, and quit. Then delete the com.###.AddressBook.plist, but keep the AdressBookData.plist. Re-run the program and nothing shows up. The if statement returns NO, because your pointer has a value after alloc.


Here is what I used to solve the problem:


records = [[NSMutableArray alloc] initWithArray:[prefs arrayForKey:@"Addresses"]];
if ( nil == [records count]) {
records = [[NSMutableArray alloc] initWithContentsOfFile:recordsFile];
if ( nil == [records count] ) {
records = [[NSMutableArray alloc] init];
} else {
[self saveData];
}
}
}


The else { [self saveData] } serves to re create the preferences file from the AddressBookData.plist file if it is missing.


Great articles, I am learning a lot.
Jeremy


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

    • 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