 |
 |
encodeObject forKey help...please
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
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
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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)encodeWithCoder  NSCoder *)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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|