views:

103

answers:

2

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 the [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
A: 

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.

Max Seelemann
that's for sure.I tried this [aCoder encodeObject:self.levelPoints forKey: @"myKey"] as well on .m implementation file, I couldn't get the data by self.levelPoints = [aDecoder decodeObjectForKey: @"myKey"];Maybe there are something else beside, I'll try again today.Thanks
A: 

I made a simple class Shape, trying to get the data from NSLog(...)s. but I got an EXC_BAD_ACCESS, please help. thanks

#import <Foundation/Foundation.h>
#define DATAPATH [NSString stringWithFormat:@"%@/Documents/mydata.archive", NSHomeDirectory()]

@interface Shape : NSObject <NSCoding> {
    NSMutableDictionary *_dic;
    NSString *_str;
}
@property (nonatomic, retain) NSMutableDictionary *dic;
@property (nonatomic, retain) NSString *str;
-(id)init;
- (id) initWithCoder: (NSCoder *)coder;
- (void) encodeWithCoder: (NSCoder *)coder;

@end


@implementation Shape

@synthesize dic = _dic;
@synthesize str = _str;

-(id)init {
    if ((self = [super init])) {
        _dic = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (id) initWithCoder: (NSCoder *)coder
{
    if (self = [super init])
    {
        _dic = [coder decodeObjectForKey:@"test"];
    }
    return self;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
    [coder encodeObject:_dic forKey:@"test"];
}

-(void) dealloc {
    [_dic release];
    [super dealloc];
}
@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Shape *shap1 = [[Shape alloc] init];
    NSMutableDictionary *_dic = [[NSMutableDictionary alloc] init];
    [_dic setObject:[NSNumber numberWithInt:1] forKey:@"a"];
    [_dic setObject:[NSNumber numberWithInt:2] forKey:@"b"];
    [_dic setObject:[NSNumber numberWithInt:3] forKey:@"c"];

    shap1.dic = _dic;
    shap1.str = @"good";

    NSMutableDictionary *projectDictionary =  [NSMutableDictionary dictionary];
    [projectDictionary setObject:shap1 forKey:@"test"];
    [NSKeyedArchiver archiveRootObject:projectDictionary toFile:DATAPATH];    

    NSMutableDictionary *projectDictionary1 = [NSKeyedUnarchiver unarchiveObjectWithFile:DATAPATH];
    Shape *shap2 = [projectDictionary1 objectForKey:@"test"];
    NSMutableDictionary *_dic2 = shap2.dic;
    NSLog(@"%@", shap2.str);
    NSLog(@"%d", [_dic2 objectForKey:@"a"]);
    NSLog(@"%d", [_dic2 objectForKey:@"b"]);
    NSLog(@"%d", [_dic2 objectForKey:@"c"]);

    [shap1 release];
    [shap2 release];
    [_dic release];
    [_dic2 release];
    [pool drain];
    return 0;
}