1

i tried to test NSKeyedArchiver, but my code seems to be wrong. I then tried only with a NSLog, but the NSLog returns (null) if i alloc-init my var "model" in the init method (in the controller), and it makes the app crash if i put it in viewDidLoad.

Can someone have a look and tell me if something is wrong?

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@class Model;

@interface AAViewController : UIViewController {
    Model *model;
}

@property (nonatomic, retain) Model *model;

//+(BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;

@end

______

#import "AAViewController.h"
#import "Model.h"

@implementation AAViewController

@synthesize model;

- (id) init {
    self = [super init];
    if (self) {
        model = [[Model alloc] initWithName:@"thomas" age:13];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //self.model = [[Model alloc] initWithName:@"thomas" age:13];
    NSLog (@"%@", self.model);
    /*
     if (![NSKeyedArchiver archiveRootObject:model toFile:@"test.archive"]){
     NSLog (@"erreur");
     [model release];
     }
     */
    NSLog(@"success !");
}

- (void)viewDidUnload {
    //model = nil;
}

- (void)dealloc {
    [model release];
    [super dealloc];
}

@end


______


#import <Foundation/Foundation.h>

//@interface Model : NSObject <NSCoding>

@interface Model : NSObject {
    NSString *name;
    int age;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic) int age;

- (id) initWithName:(NSString *)theName age:(int)theAge;

/*
 - (id) initWithCoder:(NSCoder *)encoder;
 - (void) encodeWithCoder:(NSCoder *)decoder;
 */

@end


______

#import "Model.h"

@implementation Model

@synthesize name, age;

- (id) initWithName:(NSString *)theName age:(int)theAge {
    self = [super init];
    if (self) {
        name = [theName copy];
        age = theAge;
    }
    return self;
}

- (void) dealloc {
    [name release];
    [super dealloc];
}

/*
 - (id) initWithCoder:(NSCoder *)decoder{
 self = [super init];
 if (self) {
 name = [[decoder decodeObjectForKey:@"nom"] retain];
 age = [decoder decodeIntForKey:@"age"];
 }
 return self;
 }

 - (void) encodeWithCoder:(NSCoder *)encoder
 {
 [encoder encodeObject:name forKey:@"nom"];
 [encoder encodeInt:age forKey:@"age"];
 }
 */

- (NSString *) description {
    return [NSString stringWithFormat:@"the name is : %@, %@ years old", name, age];
}

@end
Paul
  • 6,108
  • 14
  • 72
  • 128

1 Answers1

2

Your app is crashing because this line is wrong:

return [NSString stringWithFormat:@"the name is : %@, %@ years old", name, age];

age is an int, not an object pointer. Needs to be:

return [NSString stringWithFormat:@"the name is : %@, %d years old", name, age];

I don't think there's anything obviously wrong with your encode/decode code.

Flyingdiver
  • 2,142
  • 13
  • 18
  • thanks! you were right, i still have 2 things : first, i've got 2 warnings that say "imcomplete implementation of AAViewController" and "method for "archiveRootObject..." not found. i added the foundation framework into the .m file but i've still got the same warnings. The second thing is : why does it not work if i write `model = [[[Model alloc] initWithName:@"thomas" age:13] autorelease];` in the init method? (the NSLog returns (null) in this case) Thanks a lot – Paul Sep 07 '11 at 15:33
  • `model = [[[Model alloc] initWithName:@"thomas" age:13] autorelease]` doesn't work in init because you're not retaining the Model object. Get rid of the autorelease, or use a setter that does a retain. – Flyingdiver Sep 07 '11 at 19:00
  • it does not work without the autorelease, i think it is retained isn't it? alloc-init adds +1, so model is not allocated yet in init, alloc-init adds +1 and autorelease will release it. I don't understand why it's not working. – Paul Sep 07 '11 at 21:03
  • I can't tell what you're really doing, because half the relevant code in the example above is commented out. Post the actual code you're testing and the actual results and I'll look again. – Flyingdiver Sep 08 '11 at 02:15
  • thanks for your help, ok what i've learned is that "init" is not the constructor method for uiviewController, i should have used initWithNibName or initWithCoder, depending on if i use a .xib or not. Thanks for your help anyway, Cheers ! – Paul Sep 08 '11 at 23:34