Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mac > BOOK: Beginning Mac OS X Programming
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 4th, 2006, 01:38 AM
Registered User
Join Date: Mar 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default page 261 +(id) sharedAddressBook

relevant 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.
  #2 (permalink)  
Old June 3rd, 2006, 01:02 PM
Wrox Author
Join Date: Nov 2005
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts

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!


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 04:28 PM.

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