对象内的对象的NSKeyedArchiver archiveRootObject不起作用!

时间:2010-08-08 09:16:39

标签: iphone objective-c

我有一个Level类,包含一些属性的Class LevelPoints,如何使用

归档Level的实例
[projectDictionary setObject:level forKey:@"testlevel"];
[NSKeyedArchiver archiveRootObject:projectDictionary toFile:@"somewhere"];

我无法使用[NSKeyedUnarchiver unarchiveObjectWithFile:DATAPATH]中的值获取_levelPoints变量;

有什么想法吗?

#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

1 个答案:

答案 0 :(得分:0)

你的意思是:

- (id)initWithCoder:(NSCoder *)aDecoder
{
  ...
  self.levelPoints = [aDecoder decodeObjectForKey: @"myKey"];
  ...
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
  ...
  [aCoder encodeObject:self.levelPoints forKey: @"myKey"];
  ...
}

编辑:这些实现应该进入Level类。