NSKeyedArchiver 和描述方法:应用程序崩溃

发布于 2024-12-03 12:19:11 字数 2391 浏览 3 评论 0原文

我尝试测试 NSKeyedArchiver,但我的代码似乎是错误的。然后,我只尝试使用 NSLog,但是如果我在 init 方法中分配初始化我的 var“model”,则 NSLog 返回 (null) (在控制器中),如果我将其放入 viewDidLoad 中,它会使应用程序崩溃。

有人可以看一下并告诉我是否有问题吗?

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2024-12-10 12:19:11

您的应用程序正在崩溃,因为这一行是错误的:

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

age 是一个 int,而不是一个对象指针。需要:

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

我认为您的编码/解码代码没有任何明显的错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文