Re: Really struggling with Saving in NSDocument
Re: Really struggling with Saving in NSDocument
- Subject: Re: Really struggling with Saving in NSDocument
- From: Ian Jackson <email@hidden>
- Date: Wed, 27 Jul 2005 20:24:00 +1200
Yeah, I've been through the documentation for archives and
serializations as best I can. I think I need to clarify a few things
about the code - you're right.
With this app, I'm not trying to achieve a whole lot. I just want to
teach my self the ins and outs of the document architecture,
including a window controller. Quite often, the examples you come
across show you how to make a text editor, and tell you how great it
is cause it's so easy. The problem is, it's so easy, you don't learn
very much. So I just want my document type app to draw lines
corresponding to where the mouse is clicked. Actually it already does
this fine, but right from the start, my goal has been to get it to
open, save, revert, use page setup and print properly.
I wanted to use the MVC approach that seems to be so important, so I
have the view, which basically just has the drawRect and a mouseUp
method, which passes the eventLocation to my drawController class.
The drawController just has to created instances (called graphic) of
my drawingModel class which create a NSBezierPath based around the
mouseUp location, and put them in an array (called graphics) which is
created by the MyDocument class.
When I try to save, I have loads of breakpoints, and can follow what
goes on OK. The dataOfType method is called:
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
return [NSKeyedArchiver archivedDataWithRootObject:[self
graphics]];
}
It gets the array 'graphics' and then calls the encodeWithCoder
method which is in the drawingModel class, for thePath.
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject: [self linePath] forKey:@"thePath"];
return;
}
But if I try and put an encodeWithCoder method in the drawController
class, it never gets used. I don't suppose there's any reason why it
should, although that is where the graphic instances are created. I
feel like the encodeWithCoder method may as well go in MyDocument.m,
but I don't think that works.
It seems like encoding 'thePath' has to stay in the drawingModel
class, and as such I don't see how I can create a new key every time.
All the graphic instances are in the graphics array anyway. I wish I
could just archive the array and that would be the end of it.
I think that covers what the app does, and how. I hope it helps you
help me!
Ian.
On 26/07/2005, at 10:56 PM, SA Dev wrote:
Ian:
You'll want to read into the NSCoding protocol's documentation.
Any classes you try to code with NSCoder must implement the
NSCoding protocol. NSBezierPaths are compliant, so that's not the
problem there, but that may apply to your other objects as well.
Just a heads-up.
The problem you're running into is most likely the 'saving
everything as the linePath key' issue. It's not entirely clear what
contains what from your post (you may need to clarify), but if
you're saving multiple *anything*, you should throw them in an
array, then store the *array* as something like "linePaths" (note
the plural form). Each time you say [someDictionary
setObject:someObject forKey:@"theSameKeyEachTime"], you're
replacing what was already stored as theSameKeyEachTime with the
new 'someObject'.
I hope this helps. If this isn't what you're talking about,
please provide a detailed description of your objects and how
you're storing them.
On Jul 26, 2005, at 5:37 AM, Ian Jackson wrote:
Hi All,
How's it going? I've been battling away for weeks now, trying to
save a NSMutableArray of my objects (graphic). The useful part of
my graphic class is an NSBezierPath.
The array is owned by MyDocument, and my graphic objects are
created in a class I wrote, which just creates the objects and
puts them in the array (it does this in response to mouse clicks
in the view) (supposed to be the controller). So I did the keyed
archiving for the paths in my graphic objects, and the Tiger style
saving method in MyDocument. It appears to save fine, but when I
open the file that's created, the paths are no longer paths. The
debugger shows that the path has crazy numbers associated with the
bounds and origin etc. I wish I had a specific question as to
where this was going wrong, but I really don't know where the
problem lies. Here are a few things I don't yet grasp:
at the momement all the paths are encoded with the same key name.
[coder encodeObject: [self linePath] forKey:@"thePath"];
If this is wrong, I'm not sure how to go about making new names
for every path (one path per graphic object).
Secondly, I don't encode my actual graphic objects anywhere, and
I'm not sure where I'd do that if it's necessary. In MyDocument?
How does the decoder know to go though the array and decode all
the paths? The array does appear to have the right amount of
objects in it when it is decoded, and it says they are of the
class corresponding to the graphic instances. I would so love to
be able to save these lines, and open them again, and get on with
doing the program I want to create!
Thanks in advance,
Ian.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40silentalcove.net
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden