NSKeyedArchiver archiveRootObject for an object inside an object doesn't work!
I have a Level class, witch contains another Class LevelPoints for some attributes, how can I archive the instance of Level by using
[projectDictionary setObject:level forKey:@"testlevel"];
[NSKeyedArchiver archiveRootObject:projectDictionary toFile:@"somewhere"];
I can not get the _levelPoints variable with values out of th开发者_如何转开发e [NSKeyedUnarchiver unarchiveObjectWithFile:DATAPATH];
Any ideas?
#import <Foundation/Foundation.h>
@interface Level : NSObject <NSCoding> {
int _levelNum;
float _spawnRate;
NSString *_bgImageName;
LevelPoints *_levelPoints;
}
@property (nonatomic, assign) int levelNum;
@property (nonatomic, assign) float spawnRate;
@property (nonatomic, copy) NSString *bgImageName;
@property (nonatomic, retain) LevelPoints *levelPoints;
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName;
- (id)initWithCoder:(NSCoder *)aDecoder;
- (void)encodeWithCoder:(NSCoder *)aCoder;
@end
#import <Foundation/Foundation.h>
@interface LevelPoints : NSObject <NSCoding> {
int _score;
int _outflows;
int _mistakes;
}
@property (nonatomic, assign) int score;
@property (nonatomic, assign) int outflows;
@property (nonatomic, assign) int mistakes;
- (id)initWithCoder:(NSCoder *)aDecoder;
- (void)encodeWithCoder:(NSCoder *)aCoder;
@end
Do you mean something like:
- (id)initWithCoder:(NSCoder *)aDecoder
{
...
self.levelPoints = [aDecoder decodeObjectForKey: @"myKey"];
...
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
...
[aCoder encodeObject:self.levelPoints forKey: @"myKey"];
...
}
EDIT: these implementations should go into the Level class.
精彩评论