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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to