Totally stumped on extern NSString
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