• 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: Question about Saving a document
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about Saving a document


  • Subject: Re: Question about Saving a document
  • From: Paul Lynch <email@hidden>
  • Date: Mon, 5 Jun 2006 16:28:48 +0100


On 5 Jun 2006, at 15:16, Francis Derive wrote:

Now, I have no longer an error panel saying it was not possible to save the document : it "saves" without notice.
But when opening the document, the NSTextField isn't filled with its "saved" content.
Editing the "saved" content shows something like
typedstreamÅËÑ@Ö
which is the same rubbish content from the previous saves.

That's not "rubbish", that's your saved object.

On Jun 5, 2006, at 12:50 PM, Paul Lynch wrote:

On 5 Jun 2006, at 11:42, Francis Derive wrote:

My document nib has :
	- an instance of a simple model object,

You don't want to have your model instantiated in the nib, in general,

My model is quite closed to the Converter model object of Apple's Documentation Cocoa Bindings Tutorial - a specialized converter, we could say it is the Cocoa Bindings Tutorial Converter object.
So it is there, in the document nib file, with bindings to its controller object, as there are bindings between the controller object and the interface NSTextField.

The problem with putting model classes in nibs is that you have to treat them as hybrid model/controller classes. You can't reasonably replace the nib instantiated object with one that you retrieve from an archive, as you have found. Instead, you have to extract the contents (ie, the real model) and configure your pseudo-model objects with these values.


Create an instance variable in your document subclass and set it to the model object. Then you can access it in the dataOfType:/ readFromData: methods.

Paul


This is the document subclass :

		#import <Cocoa/Cocoa.h>
		#import "AToNconverter.h"
		@interface MyDocument : NSDocument {
			AtoNconverter *decodedObject;

That's not a very helpful name for a "model" object :-).

		}
		- (void) setDecodedObject:(AtoNconverter *) theThing;
		- (AtoNconverter *) decodedObject;

		@end

		I have overwritten :

- (NSData *) dataOfType:(NSString *)typeName error:(NSError **) outError {
return [NSArchiver archivedDataWithRootObject:[self valueForKey:@"decodedObject"]];
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
[self setValue:[NSUnarchiver unarchiveObjectWithData:data] forKey:@"decodedObject"];

This is where you need to load it into a local variable, and configure the nib version with values from it - presumably setAsciiToConvert:, eg:


	AtoNconverter *saved = [NSUnarchiver unarchiveObjectWithData:data];
	[decodedObject setAsciiToConvert:[saved asciiToConvert]];

			return YES;
		}


And this is the model subclass :

@interface AtoNconverter : NSObject <NSCoding> { // I notice for myself the NSCoding protocol
NSString * asciiToConvert;
}


		- (NSString *) translatedAddress;
		- (NSString *) asciiToConvert;
		- (void) setAsciiToConvert:(NSString *) newAscii;

		@end

		I have overwritten :

			- (id) initWithCoder:(NSCoder *) coder
			- (void) encodeWithCoder:(NSCoder *)coder {
				if ([coder allowsKeyedCoding]) {

It is only really worthwhile doing this check if you released your app under 10.2 or earlier (or whenever keyed encoding was introduced). For all modern apps, just assume keyedCoding.


					[coder encodeObject:asciiToConvert forKey:@"asciiToConvert"];
				} else {
					[coder encodeObject:asciiToConvert];
				}
			}

A long way to be the great Cocoa scholar !

Encore merci.

Paul


_______________________________________________ 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
References: 
 >Question about Saving a document (From: Francis Derive <email@hidden>)
 >Re: Question about Saving a document (From: Paul Lynch <email@hidden>)
 >Re: Question about Saving a document (From: Francis Derive <email@hidden>)

  • Prev by Date: which cocoa wrapper for sqlite should i get?
  • Next by Date: Re: which cocoa wrapper for sqlite should i get?
  • Previous by thread: Re: Question about Saving a document
  • Next by thread: Re: Question about Saving a document
  • Index(es):
    • Date
    • Thread