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 > Saving Panel Data

Saving Panel Data
Thread Tools
tikki
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
May 31, 2003, 10:41 AM
 
Greetings,

I created a panel with a tabView embedded in it. The data contained in the panel is unique to each file saved by the user.

I cannot figure out how to write this data to the file I am saving with my other data. I assume I should save the data each time the panel closes, but I dunno where to go from there.

Any help?

work: maczealots blog: carpeaqua
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jun 1, 2003, 08:11 AM
 
What kind of data are we talking about? Check out NSCoding...

http://developer.apple.com/techpubs/.../NSCoding.html

Steve W
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Jun 1, 2003, 09:46 AM
 
I am usind NSCoding for all of my data in tableviews. I have attached a screenshot of what i am trying to save.



I created a class that has a variable for each item on that panel, but I don't really know how I should tie it into my MyDocument class so the data saves.

Am I making any sense?

Thanks for the help.

work: maczealots blog: carpeaqua
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Jun 1, 2003, 11:56 PM
 
I think I need to subclass NSWindowController. ugh.

work: maczealots blog: carpeaqua
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jun 2, 2003, 12:12 AM
 
Originally posted by tikki:
I think I need to subclass NSWindowController. ugh.
are you using encodeWithCoder and initWithCoder to save and retrieve your documents?

If so, you must have some single class that is being encoded/decoded?

And you're storing the contents of each of those fields in an NSString or something like that?

If so you'll have an initWithCoder and encodeWithCoder method in the class implementation that is being saved, something like:

Code:
- (id)initWithCoder:(NSCoder *)coder { [self setCourseName:[coder decodeObject]]; [self setLocation:[coder decodeObject]]; [coder decodeValueOfObjCType:"i" at:&numberOfPeople]; return self; } - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:[self courseName]]; [coder encodeObject:[self location]]; [coder encodeValueOfObjCType:"i" at:&numberOfPeople]; }
(This assumes that somewhere you have code that extracts data from your fields into the objects "courseName" and "location". Otherwise, you could theoretically encode/decode directly from the field content itself).

Note the two forms of encoding/decoding. One is for objects (eg, NSString, as above) the other is for C variable types (eg, integer, above).

You then invoke the decoding using something like the following:

Code:
newObject = [NSUnarchiver unarchiveObjectWithFile:filename]; if ( newObject ) { [newObject retain]; ... ... ... }
and invoke the encoding using somehting like:

Code:
[NSArchiver archiveRootObject:objectToSave toFile:path];
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Jun 2, 2003, 12:20 AM
 
Originally posted by Brass:
are you using encodeWithCoder and initWithCoder to save and retrieve your documents?
Yes.

If so, you must have some single class that is being encoded/decoded?

And you're storing the contents of each of those fields in an NSString or something like that?
Yes. it's an NSMutableArray that holds the values of two other NSMutableArrays (one for each table in the main window)


If so you'll have an initWithCoder and encodeWithCoder method in the class implementation that is being saved, something like:

Code:
- (id)initWithCoder:(NSCoder *)coder { [self setCourseName:[coder decodeObject]]; [self setLocation:[coder decodeObject]]; [coder decodeValueOfObjCType:"i" at:&numberOfPeople]; return self; } - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:[self courseName]]; [coder encodeObject:[self location]]; [coder encodeValueOfObjCType:"i" at:&numberOfPeople]; }
(This assumes that somewhere you have code that extracts data from your fields into the objects "courseName" and "location". Otherwise, you could theoretically encode/decode directly from the field content itself).

Note the two forms of encoding/decoding. One is for objects (eg, NSString, as above) the other is for C variable types (eg, integer, above).

You then invoke the decoding using something like the following:

Code:
newObject = [NSUnarchiver unarchiveObjectWithFile:filename]; if ( newObject ) { [newObject retain]; ... ... ... }
and invoke the encoding using somehting like:

Code:
[NSArchiver archiveRootObject:objectToSave toFile:path];
[/B][/QUOTE]

Yes, I have my STClassInformation class set up exactly like that, but how do I get all that to work in my MyDocument class.

When the user saves/opens the document, I don't know how to get the panel's data to save with the table view stuff. At this point my STClassInformation has no relation to the panel at all.

work: maczealots blog: carpeaqua
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jun 2, 2003, 12:33 AM
 
What is the data structure you're using? You mentioned that you are encoding/decoding an NSMutableArray. Is that an instance variable in your STClassInformation class?

Are you using
newObject = [NSUnarchiver unarchiveObjectWithFile:filename]
&
[NSArchiver archiveRootObject:objectToSave toFileath];

to encode/decode your STClassInformation object, or just the NSMutableArray object?

Ie, are the "newObject" and "objectToSave" in my example of which class, NSMutableArray, or STClassInformation?

Perhaps you could post some sample code?
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Jun 2, 2003, 12:46 AM
 
here's a basic throwdown of what I have.

MyDocument - saves, loads data, add/deletes rows from the two tables.

STClassInformation - class for the stuff in that panel (see screenshot above)

STGrade - each time a person adds a value to the grades table, its an instance of this. Just two NSStrings. The instances are saved in an NSMutableArray called gradeList

STAssignment - each time a person adds a value to the assignment table, its of this. Again, just more NSSTrings. The instances are saved in an NSMutableArray called assignmentList

here is how I do my encoding/decoding in each of those three previous classes.

Code:
- (id)initWithCoder:(NSCoder *)coder { [super init]; [self setAssignmentType:[coder decodeObject]]; [self setGrade:[coder decodeObject]]; return self; } - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:[self assignmentType]]; [coder encodeObject:[self grade]]; }
Here is my saving stuff. At this point it basically takes assignmentList and gradeList and consolidates them into an NSMutableArray called dataStuff (for lack of a better phrasing).

Code:
- (NSData *) dataRepresentationOfType:(NSString *)aType { [oTableView deselectAll:nil]; _dataStuff = [[NSMutableDictionary alloc] init]; [_dataStuff setObject:_assignmentList forKey:@"assignmentListKey"]; [_dataStuff setObject:_gradeList forKey:@"gradeListKey"]; return [NSArchiver archivedDataWithRootObject:_dataStuff]; }
I doubt its the most elegant code you've ever seen, but its a start.

work: maczealots blog: carpeaqua
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jun 2, 2003, 12:53 AM
 
It's a little different to how I'd do it, but is there any reason why you can't just add the other items to your NSMutableDictionary?

Eg

Code:
_dataStuff = [[NSMutableDictionary alloc] init]; [_dataStuff setObject:_assignmentList forKey:@"assignmentListKey"]; [_dataStuff setObject:_gradeList forKey:@"gradeListKey"]; [_dataStuff setObject:_courseName forKey:@"courseNameKey"]; [_dataStuff setObject:_location forKey:@"locationKey"];
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Jun 2, 2003, 12:58 AM
 
Doesn't that entail just creating a bunch of outlets to the Textfields in my MyDocument class?

If I am understanding correctly, I would just forget about my STClassInformation?

work: maczealots blog: carpeaqua
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jun 2, 2003, 01:07 AM
 
Originally posted by tikki:
Doesn't that entail just creating a bunch of outlets to the Textfields in my MyDocument class?

If I am understanding correctly, I would just forget about my STClassInformation?
Either way would work. I can't recommend one way or the other, because I don't know what you're planning on doing in the rest of your code.

However, what I would usually do, is instead of using an NSMutableArray, I would create another class (perhaps this is what your STClassInfo is) which stores all of those items in it's instance variables. Then instead of archiving/unarchiving an NSMutableArray, you archive/unarchive an STClassInfo object.

Also, I have to admit that I'm not sure what you're using the "dataRepresentationOfType" method for. I've never used that in archiving/unarchiving anything.
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Jun 2, 2003, 01:15 AM
 
Originally posted by Brass:

Also, I have to admit that I'm not sure what you're using the "dataRepresentationOfType" method for. I've never used that in archiving/unarchiving anything.
Probably because I don't know what I am doing

work: maczealots blog: carpeaqua
     
   
 
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 04:50 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.,