Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Mac > BOOK: Beginning Mac OS X Programming
|
BOOK: Beginning Mac OS X Programming
This is the forum to discuss the Wrox book Beginning Mac OS X Programming by Michael Trent, Drew McCormack; ISBN: 9780764573996
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Mac OS X Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 4th, 2006, 01:38 AM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default page 261 +(id) sharedAddressBook

relevant code:
Code:
+(id)sharedAddressBook {
    static AddressBook *sharedAddressBook = nil;
    if ( ! sharedAddressBook ) {
        // Load from file if the file exists
        NSFileManager *fm = [NSFileManager defaultManager];
        if ( [fm fileExistsAtPath:AddressBookFilePath] ) {
            sharedAddressBook = [[AddressBook alloc] initWithFile:AddressBookFilePath];
        }
        else { // Create a new AddressBook
            sharedAddressBook = [[AddressBook alloc] init];
        }
    }
    return sharedAddressBook;
}
it confuses me that every call to sharedAddressBook starts with:
static AddressBook *sharedAddressBook = nil;
if ( ! sharedAddressBook ) {
...
}
this seems like it will always be evaluated, and the address book will be reinitialized every time...

please let me know what I'm missing here.
 
Old June 3rd, 2006, 01:02 PM
Wrox Author
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you are missing here is the role of the "static" keyword. The explanation on Page 264 should clear things up for you:

"A static variable called sharedAddressBook is declared, and initialized to nil. Being declared static means that it will not disappear when the method returns, but will remain for the life of the program. The if statement checks to see if the variable is nil; if not, it simply returns teh AddressBook to the calling code. If the variable is nil, a new AddressBook must be created."

So, the if statement *will* always be evaluated -- on that point you are correct. It's just that the value of sharedAddressBook will be nil only on the first call to the function. Then sharedAddressBook will be assigned a non-nil value, and that value will be remembered for the life of the program.

Hope that helps!






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to obtain - for example(page.aspx?id=12) preetham.sarojavenkatesh Visual Studio 2005 4 September 11th, 2007 12:47 PM
Pass ID to redirect page? okboy ASP.NET 2.0 Basics 2 December 14th, 2006 06:12 PM
ID column used to redirect page rdove84 ASP.NET 1.0 and 1.1 Basics 11 November 14th, 2006 05:10 PM
ZVON Tutorial Page 56 id() function scubaduba XSLT 1 November 1st, 2004 12:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.