Re: Newbie Q : Not-so-simple encoding&decoding example
Re: Newbie Q : Not-so-simple encoding&decoding example
- Subject: Re: Newbie Q : Not-so-simple encoding&decoding example
- From: James Bucanek <email@hidden>
- Date: Fri, 26 May 2006 12:06:19 -0700
email@hidden wrote on Friday, May 26, 2006:
> I've read many tutorials on the way to encode and decode objects in Cocoa,
>but they all share one limitation : they only treat the simplest
>"one class-one data type" case. When I tried to adapt it to the slightly more
>complicated example below I failed. Can anyone please tell me what should
>be corrected
>in the encoding & decoding methods below ?
I'm not sure what the problem is, but I have a few comments.
<clip>
>In Class1.m
>
>-- (id)initWithCoder:(NSCoder *)coder
>{
> if (self = [super init]) {
> [array1 release];
> [array2 release];
These are pointless releases. You just called -[super init]. All of your instance variables are guaranteed to be nil. There is no point in sending release messages to object pointer you know are nil.
> array1=[[coder decodeObject] retain];
> array2=[[coder decodeObject] retain];
>
> }
> return self;
>}
You are using the olde style of archiving. You only need non-keyed encoding if you're writing for OS 10.1. Use keyed-value encoding and decoding:
array1 = [[coder decodeObjectForKey:@"array1"] retain];
array2 = [[coder decodeObjectForKey:@"array2"] retain];
>
>- (void)encodeWithCoder:(NSCoder *)coder
>{
> [coder encodeObject:array1];
> [coder encodeObject:array2];
and:
[coder encodeObject:array1 forKey:@"array1"];
[coder encodeObject:array2 forKey:@"array2"];
Refer to the current ADC Reference documentation "Archives and Serializations Programming Guides for Cocoa" for the details and example code. If you developed your code from examples in a book, then it suggest that the book is dreadfully out of date. I'd look for a more recent one.
Are you absolutely positive that all of the members of array1 and array2 are NSCoder compliant? If any of the members of array1 or array2 can't be encoded, then the NSArray won't be encoded.
If the encoder or decoder is throwing an exception, simply step through your code using the debugger and find out which object is causing the problem.
--
James Bucanek
_______________________________________________
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