• 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: Moving to Keyed Archiving
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Moving to Keyed Archiving


  • Subject: Re: Moving to Keyed Archiving
  • From: Drew McCormack <email@hidden>
  • Date: Wed, 27 Aug 2003 13:17:26 +0200

On Tuesday, August 26, 2003, at 09:32 PM, Drew McCormack wrote:

On Tuesday, August 26, 2003, at 05:58 PM, Justin Anderson wrote:

There's one idea provided by Apple in the Cocoa Docs at <http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/ Tasks/convertingclasses.html>.

What you're looking for is down towards the bottom of the page, right before the sample code. The gist of it is that you should test for -containsValueForKey: (available in both NSKeyedUnarchiver and NSCoder). Apple's sample code on there is for NSCoder's -initWithCoder, but here's basically what you'd need to do for your -loadDataRepresentation: ofType:

- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
NSKeyedUnarchiver *keyedUnarchiver = [[[NSKeyedUnarchiver alloc]
initForReadingWithData:data] autorelease];
NSUnarchiver *unarchiver = [[[NSUnarchiver alloc]
initForReadingWithData:data] autorelease];

Unfortunately this doesn't work. If the data is not in keyed format, you get the following


2003-08-26 21:21:25.592 Trade Strategist[13644] CFLog (0):
CFPropertyListCreateFromXMLData(): plist parse failed; the data is not proper UTF-8. The file name for this data could be:
com.apple.LaunchServices.00039363bc02.plist -- /Users/drew/Library/Preferences/ByHost/
The parser will retry as in 10.1, but the problem should be corrected in the plist.

Trade Strategist has exited due to signal 11 (SIGSEGV).


Unfortunately you don't get a nil object returned, or an NSException raised, it just dies with a signal 11. Bad.
How can I get around this? I need to be able to find out what is in the data without crashing my program.

Drew

------
To answer my own post, I did manage to come up with a solution. Here it is, in case it helps someone.

If you try to initialize an NSKeyedUnarchiver with non-keyed data, it will crash. Bad Apple!!! I regard this as a bug. You should get a chance to recover, via an NSException or by returning nil.

Luckily the NSUnarchiver does not crash if it tries to decode invalid data. A method like decodeObject simply returns nil, as it should.

So the solution to knowing whether you are dealing with serial or keyed data, is first to try the serial version in your NSDocument's loadDataRepresentation:ofType: method. If the decodeObject methods are returning nil, the data is not serial. You can then try the keyed version safely without crashing your code.

Drew

----------------------------------
Dr. Drew McCormack
Trade Strategist (www.trade-strategist.com)
Stock Market strategy design platform for Mac OS X.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Moving to Keyed Archiving
      • From: Chris Kane <email@hidden>
References: 
 >Re: Moving to Keyed Archiving (From: Drew McCormack <email@hidden>)

  • Prev by Date: NSColorWell and NSColor Category
  • Next by Date: (no subject)
  • Previous by thread: Re: Moving to Keyed Archiving
  • Next by thread: Re: Moving to Keyed Archiving
  • Index(es):
    • Date
    • Thread