Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > encodeObject forKey help...please

encodeObject forKey help...please
Thread Tools
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Dec 2, 2002, 06:58 PM
 
OK, I am about to release my a new application which encodes instance variables in a model class. Before I release it, I want to make sure that there is backward compatability with saved files, because I am sure in the future I will modify the model class and its encoding and decoding methods. So, someone suggested I use the method encodeObject(id)anObject forKey:(NSString *)str

Here is my encode method:

-(void)encodeWithCoder:(NSCoder *)coder {
//name returns an NSString
[coder encodeObject: [self name] forKey:@"Name"];
//date returns an NSCalendarDate
[coder encodeObject: [self date] forKey:@"Date"];
}

Whenever I save, the first line of code in this method gives the following error:

Exception raised during posting of notification. Ignored. exception: *** -encodeObject:forKey: only defined for abstract class. Define -[NSArchiver encodeObject:forKey:]!

I should say that my model class is a subclass of NSObject and in the hearder I have this:

@interface MyModelClass : NSObject <NSCoding>

Note also that it works fine if I use the method encodeObject, it just throws the exception when I use the encodeObject forKey.

So, excuse my ignorance, but how do I "define" NSArchiver encodeObject:forKey: ?

~Thanks
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Dec 2, 2002, 10:22 PM
 
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Dec 2, 2002, 10:32 PM
 
Thanks Matt. I have read over that documentation, I get an error if I call super encodeObject forKey...

I put in the if conditional and when it saves it never uses the encodeObject: forKey: path.

According to the documentation I don't need the if conditional: "If instances of your class can only be encoded to an keyed archive, you can omit the section that encodes values using the nonkeyed methods."

So I figure this is my case, but I still get the "define..." exception.

Any other suggestions?
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Dec 2, 2002, 10:55 PM
 
Are you using NSArchiver to enode or your object? That might be your problem.

Matt
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Dec 2, 2002, 11:04 PM
 
I am not using NSArchiver to encode each instance of my object. Can you post an example of how I would do that?
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Dec 2, 2002, 11:06 PM
 
I'd simply be copying pasting the example in Apple's docs. Could you post your code? That would be easier to debug. Skeleton code is alright.

Matt
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Dec 2, 2002, 11:17 PM
 
OK, here is my encode method, which definitely gets called when I save. If I try this, I get the exception error posted above.

-(void)encodeWithCoderNSCoder *)coder {

[coder encodeObject: [self returnStringMethod] forKey:@"ItemA"];

[coder encodeObject: [self date] forKey:@"Date"];
[coder encodeObject: [self notes] forKey:@"Notes"];

[coder encodeInt:anInt forKey:@"IntOne"];

[coder encodeFloat:aFloat forKey:@"FloatOne"];

}
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Dec 2, 2002, 11:22 PM
 
Well, I guessed that. What are you calling to encode your object? That was the code I was hoping you would post in addition (sorry this is taking so long).

Matt
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Dec 2, 2002, 11:32 PM
 
Don't apologize, I am just glad someone is looking at the code and trying to help.

I hope this is what you want.

I store instances of my model class myModelClass in an NSMutableArray. In another class which is a subclass of NSDocument, I override, dataRepresentationOfType where I then store my NSMutableArray into an NSMutableDictionary and then I call:

return [NSArchiver archivedDataWithRootObject:eventDict];
//eventDict is the mutable dict.
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Dec 2, 2002, 11:41 PM
 
Oh, so you ARE using NSArchiver to archive your object. That would have cleared this up sooner. NSArchiver doesn't support keyed archiving; use NSKeyedArchiver instead.

There you go,
Matt
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Dec 2, 2002, 11:50 PM
 
Thanks Matt! It works.

So do you think if I release a new version I can just encode any new instance variables with new keys and decode them with the new keys? Do you think it will be that easy?
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Dec 2, 2002, 11:59 PM
 
For new objects, yep. It's slightly more code if your application every wrote out data without using keyed archiving, but if this is a new object, then go keyed archiving all the way and it really should be that simple.

Matt
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2