Re: Totally stumped on extern NSString
Re: Totally stumped on extern NSString
- Subject: Re: Totally stumped on extern NSString
- From: Ken Tozier <email@hidden>
- Date: Sun, 8 Jul 2007 12:43:21 -0400
Just out of curiosity, why aren't you defining the address book path
in a plist file?
Assuming a plist defaults file named "AddressBookDefaults" and
a field in that plist named: "defaultPath"
You can init the default prefs in your "initialize" function
static NSDictionary *gAddressBookDefaults = nil;
@implementation AddressBook
+ (void) initialize
{
NSBundle *bundle = [NSBundle bundleWithClass: [self class]];
NSString *defaultsPath = [bundle pathForResource:
@"AddressBookDefaults" ofType: @"plist"];
gAddressBookDefaults = [[NSDictionary dictionaryWithContentsOfFile:
defaultsPath] retain];
}
and provide an accessor to the global defaults like this
+ (id) preferenceForKey:(NSString *) inKey
{
return [gAddressBookDefaults objectForKey: inKey];
}
That way you can add any number of defaults/prefs and change them at
will without having to recompile.
Just a thought...
Ken
On Jul 7, 2007, at 7:45 PM, William Squires wrote:
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:
40comcast.net
This email sent to email@hidden
_______________________________________________
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