• 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: Why Don't Cocoa's (Un)Archiving Methods return Errors?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Re: Why Don't Cocoa's (Un)Archiving Methods return Errors? (From: Rainer Brockerhoff <email@hidden>)
 >Re: Why Don't Cocoa's (Un)Archiving Methods return Errors? (From: email@hidden)
 >Re: Why Don't Cocoa's (Un)Archiving Methods return Errors? (From: Gwynne Raskind <email@hidden>)
 >Re: Why Don't Cocoa's (Un)Archiving Methods return Errors? (From: Jerry Krinock <email@hidden>)
 >Re: Why Don't Cocoa's (Un)Archiving Methods return Errors? (From: Jens Alfke <email@hidden>)
 >Re: Why Don't Cocoa's (Un)Archiving Methods return Errors? (From: Jerry Krinock <email@hidden>)
 >Re: Why Don't Cocoa's (Un)Archiving Methods return Errors? (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Unnecessary Boolean Warning
  • Next by Date: [iOS] Block animation kills performance
  • Previous by thread: Re: Why Don't Cocoa's (Un)Archiving Methods return Errors?
  • Next by thread: What could cause a fast enumeration mutation error in updating tracking areas?
  • Index(es):
    • Date
    • Thread