Re: Newbie-Saving file in Tiger
Re: Newbie-Saving file in Tiger
- Subject: Re: Newbie-Saving file in Tiger
- From: Sherm Pendley <email@hidden>
- Date: Mon, 10 Oct 2005 17:16:27 -0400
On Oct 10, 2005, at 4:48 PM, Ricky Sharp wrote:
On Oct 10, 2005, at 12:52 PM, Steve Gran wrote:
Anyway, I'm also going thru Hillegass' excellent book and am now
on the subject of saving with NSKeyedArchiver. But in Xcode under
the dataRepresentationOfType method the following comment says
"For applications targeted for Tiger or later systems, you should
use the new Tiger API -dataOfType:error:"
Well I looked at the documentation but still don't understand how
to use this correctly. Remember I'm like a six year old when it
comes to programming.
If I'm just setting up a simple save my document to a file, what
is the correct to use this?
Would it be...
- (NSData *)dataOfType:(NSString *)aType
{
return [NSKeyedArchiver archivedDataWithRootObject:tasks];
}
That looks good to me.
Look again - the correct NSDocument method to override is -
dataOfType:error:.
- (NSData *)dataOfType: (NSString *)type error:(NSError **)error
{
NSData *theData = [NSKeyedArchiver
archivedDataWithRootObject:tasks];
// Return the data if it was created correctly
if (nil != theData) return theData;
// The caller may pass nil, so check it and create an NSError if
one was asked for
if (nil != error)
{
// Creating an NSError object is optional. If you do choose
to create one, fill it with more
// detailed information about what went wrong. Cocoa can
infer that an NSData object
// could not be created, from the fact that this method
returns nil. What the NSError does
// is give you a chance to show *why* it couldn't be created.
// It's actually rather pointless in this demo, as
NSKeyedArchiver didn't tell us why it
// returned nil. Still, it illustrates how to return more
detailed info, should such info be
// available to begin with.
// See NSError docs for details
NSDictionary *errorDictionary = [NSDictionary
dictionaryWithObject:@"Failed to
create keyed archive for unknown reason."
forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:@"invalid.whatever"
code:-1 userInfo:errorDictionary];
}
return nil;
}
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
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