Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSDocument's readFromFile

NSDocument's readFromFile
Thread Tools
cmoad
Fresh-Faced Recruit
Join Date: Dec 2000
Location: Bloomington
Status: Offline
Reply With Quote
Feb 5, 2002, 12:22 PM
 
I have a subclass of NSDocument in which I have implemented the writeToFile and readFromFile methods. The writeToFile method works fine using writeToFile:atomically: and even the readFromDocument works. I am writing and reading an NSDictionary. The problem comes with loadDocWithData. When I debug my app and open a file, all the components run fine, but no data is displayed in my new window.

- (void)loadDocWithData NSMutableDictionary *)data {
Card *card;
NSString *keyword;
int i;

if (data) {
keywordList = [NSMutableArray arrayWithArray:[data allKeys]];

for (i = 0; i < [data count]; i++) {
keyword = [keywordList objectAtIndex:i];
NSLog(keyword);
NSLog([data objectForKey:keyword]);
card = [[[Card alloc] init] autorelease];
[card setWord:keyword];
[card setDefinition:[data objectForKey:keyword]];
[keywordDict setObject:card forKey:keyword];
}

[keywordTable reloadData];
[keywordTable selectRow:[keywordList indexOfObject:keyword] byExtendingSelection:NO];
}
}

The program works perfect when a new file is used, but when I try to open a saved file that I know has content, the new interface seems to be reset.
Any ideas on what the problem is???
Thanks,
Charlie

[ 02-05-2002: Message edited by: cmoad ]

[ 02-05-2002: Message edited by: cmoad ]
     
Apocalypse
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Feb 6, 2002, 09:14 AM
 
I had a problem similar to this once. The cause was that I was opening the file and trying to display the data in the NIB before the NIB finished loading.

As I recall, the solution involved staring the data in a structure that is in the object opening the file. Then you have to implement the "awakeFromNib" or some other Nib load notification method to see if any data is waiting to be displayed and, if there is, draw the contents where it should be.

Try that out,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
lindberg
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Feb 6, 2002, 06:32 PM
 
Yup. -readFromFile: is called around document creation, but the nib isn't loaded until the window needs to be placed on screen, which happens later in the normal scheme of things. Therefore your keywordTable instance variable hasn't been set yet, and is nil.

You want the -reloadData call etc. after the nib is loaded. Putting it in awakeFromNib would work, though in an NSDocument subclass overriding windowControllerDidLoadNib: is conceptually a bit better. [In NSWindowController subclasses, override -windowDidLoad.] You probably want to call [super windowControllerDidLoadNib:theController] first for style points as well.
     
cmoad  (op)
Fresh-Faced Recruit
Join Date: Dec 2000
Location: Bloomington
Status: Offline
Reply With Quote
Feb 6, 2002, 09:40 PM
 
Thanks for the replies. I have changed the program per your recommendations and it does indeed work.
Thanks again,
Charlie
     
   
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 07:04 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,