Re: Why Don't Cocoa's (Un)Archiving Methods return Errors?
Re: Why Don't Cocoa's (Un)Archiving Methods return Errors?
- Subject: Re: Why Don't Cocoa's (Un)Archiving Methods return Errors?
- From: Jerry Krinock <email@hidden>
- Date: Sun, 31 Jul 2011 15:53:27 -0700
On 2011 Jul 30, at 23:00, Jens Alfke wrote:
>
> On Jul 30, 2011, at 10:21 PM, Jerry Krinock wrote:
>
>> I don't think so, Jens. "They" is Apple. Apple has the source code for -initWithCoder: and -encodeWithCoder:.
>
> No they don’t — not for the implementations of those methods in our own classes…
Thank you, Jens. I see the problem.
Here's a fairly radical solution:
@interface NSObject (LookMaNoEncodingExceptions)
- (void)encodeWithCoder:(NSCoder*)coder ;
- (id)initWithCoder:(NSCoder*)decoder ;
@end
@implementation NSObject (LookMaNoEncodingExceptions)
- (void)encodeWithCoder:(NSCoder*)coder {
NSString* archivoid = [NSString stringWithFormat:
@"Sorry, %@ is not encodable.\n"
@"Here's a description of it: %@",
[self className],
[self description]] ;
[coder encodeObject:archivoid
forKey:@"Archivoid"];
}
- (id)initWithCoder:(NSCoder*)coder {
return [[coder decodeObjectForKey:@"Archivoid"] retain] ;
}
@end
******************** Test Code ************************
@interface UnencodableFoo : NSObject
@end
@implementation UnencodableFoo
@end
// And then, later,
id unencodable = [[[UnencodableFoo alloc] init] autorelease] ;
NSDictionary* info = [NSDictionary dictionaryWithObject:unencodable
forKey:@"The Foo"] ;
NSError* error = [NSError errorWithDomain:@"MyDomain"
code:12345
userInfo:info] ;
// The following would raise an exception were it not for
// NSObject(LookMaNoEncodingExceptions) being linked in.
NSData* archive = [NSKeyedArchiver archivedDataWithRootObject:error] ;
// OK, now reverse the process
NSError* unError = [NSKeyedUnarchiver unarchiveObjectWithData:archive] ;
NSLog(@"Unarchived userInfo: %@", [unError userInfo]) ;
******************* Output ***************************
2011-07-31 15:30:02.097 TestTool[11645:10f] Unarchived userInfo: {
"The Foo" = "Sorry, UnencodableFoo is not encodable.
Here's a description of it: <UnencodableFoo: 0xa8a450>" ;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden