• 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
Re: Totally stumped on extern NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Totally stumped on extern NSString


  • Subject: Re: Totally stumped on extern NSString
  • From: Steven Noyes <email@hidden>
  • Date: Sun, 8 Jul 2007 09:12:59 -0700

Based on your code snippets, you would expect a crash.

You have defined and declared AddressBookFilePath here:

NSString *AddressBookFilePath;

but that does not allocate an instance of that object. Note that this object is not contained within any other object instance. Some place, you will need to do a:


AddressBookFilePath = [[NSString alloc] initWithString:@"SomeFilePath];

This will return assign a retained string to AddresBookFilePath. NOTE: I personally am conserned with this design because it is not "object oriented" with good encapsulation of the data. I would hope AddressBookFilePath would be accessed through an accessor:

filePath = [someController addressBookFilePath];
[someController setAddressBookFilePath:filePath];

NOTE: someController might be a class with only class methods and all variables stored at global scope.

NOTE1: nitpick. Capitalization. Cocoa follows the convention that classes have the first letter capitalized and instances (like AddressBookFilePath) have their first letter lowercase. This can greatly aid in maintainability.

Steven

On Jul 7, 2007, at 4: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:
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


References: 
 >Totally stumped on extern NSString (From: William Squires <email@hidden>)

  • Prev by Date: Re: NSNumber increment
  • Next by Date: Re: NSImage representations
  • Previous by thread: Re: More info:
  • Next by thread: Re: Totally stumped on extern NSString
  • Index(es):
    • Date
    • Thread