• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Totally stumped on extern NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Totally stumped on extern NSString


  • Subject: Totally stumped on extern NSString
  • From: William Squires <email@hidden>
  • Date: Sat, 7 Jul 2007 18:45:30 -0500

Okay, I'm baffled. I've got some files:

AddressBook.h & AddressBook.m

<AddressBook.h>
#import <Foundation/Foundation.h>

@class Person;

extern NSString *AddressBookFilePath;

@interface AddressBook : NSObject
{
@private
NSMutableDictionayr *personForNameDict;
}

+ (id)sharedAddressBook;
- (id)init;
- (id)initWithFile:(NSString *) path;
- (void)writeToFile:(NSString *) path;
- (void)writeSharedAddressBookToFile;
- (void)addPerson:(Person *) newPerson;
- (Person *)personForName:(NSString *) name;

@end
<End AddressBook.h>

<AddressBook.m>
#import "AddressBook.h"
#import "Person.h"

NSString *AddressBookFilePath;

@implementation AddressBook
...
- (void)writeToFile:(NSString *) path
{
NSMutableString *string = [NSMutableString string];
NSEnumerator *en = [personForNameDict objectEnumerator];
Person *person;
while (person = [en nextObject])
  {
  [string appendString:[person name];
  [string appendString:@"\n"];
  [string appendString:[person address]];
  [string appendString:@"\n"];
  }
[string writeToFile:path atomically:YES];
}

+ (void)writeSharedAddressBookToFile
{
// Added as a debug aid..
NSString *localStr = [NSString stringWithString:AddressBookFilePath];
// ...
[[AddressBook sharedAddressBook] writeToFile:localStr];
}
...
@end

(uninteresting parts trimmed for brevity... :) )

The program keeps crashing in [AddressBook writeToFile:(NSString *)] when it tries to call the line

[string writeToFile:path atomically:YES];

because path = nil, but when I click on the stack crawl to view the calling method, I can see (in the Globals Browser) that AddressBookFilePath is initialized and has a valid NSString in it! Why is the call losing the NSString pointer value?

P.S. The initial version of [AddressBook writeSharedAddressBookToFile] just had the line

[[AddressBook sharedAddressBook] writeToFile:AddressBookFilePath];

which was singularly useless as I can't view the pointer value of "AddressBookFilePath" in the Globals browser, so I put the above code in so I could try to see what was being passed in. (initially I just tried to do a shallow copy of the AddressBookFilePath variable into 'localStr', but the debugger said it was nil. This totally doesn't make sense. The code is clearly initializing the string; why can't I get the value back out? It it a compiler bug w/regards to extern pointers?

P.P.S. This is from the book, "Beginning MacOS X Programming" Chapter 7, pp 266..271

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Totally stumped on extern NSString
      • From: Ken Tozier <email@hidden>
    • Re: Totally stumped on extern NSString
      • From: Steven Noyes <email@hidden>
    • Re: Totally stumped on extern NSString
      • From: Fritz Anderson <email@hidden>
    • Re: Totally stumped on extern NSString
      • From: William Squires <email@hidden>
  • Prev by Date: RS: basic sound tool
  • Next by Date: Re: Totally stumped on extern NSString
  • Previous by thread: Re: RS: basic sound tool
  • Next by thread: Re: Totally stumped on extern NSString
  • Index(es):
    • Date
    • Thread