【问题标题】:NSKeyedArchiver and description method : app crashesNSKeyedArchiver 和描述方法:应用程序崩溃
【发布时间】:2011-09-07 13:30:38
【问题描述】:

我试图测试NSKeyedArchiver,但我的代码似乎是错误的。然后我只尝试使用NSLog,但如果我在init 方法(在控制器中)中分配初始化我的var“模型”,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

【问题讨论】:

    标签: objective-c nskeyedarchiver


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 谢谢!你是对的,我还有两件事:首先,我有 2 个警告说“AAViewController 的不完整实现”和“未找到“archiveRootObject 的方法......”。我将基础框架添加到 .m 文件中,但是我仍然收到相同的警告。第二件事是:如果我在 init 方法中写model = [[[Model alloc] initWithName:@"thomas" age:13] autorelease]; 为什么它不起作用?(在这种情况下,NSLog 返回(null))非常感谢
    • model = [[[Model alloc] initWithName:@"thomas" age:13] autorelease] 在 init 中不起作用,因为您没有保留 Model 对象。摆脱自动释放,或使用执行保留的设置器。
    • 没有自动释放它就无法工作,我认为它被保留了不是吗? alloc-init 加+1,所以模型在init 中还没有分配,alloc-init 加+1,autorelease 会释放它。我不明白为什么它不起作用。
    • 我不知道你到底在做什么,因为上面示例中的一半相关代码被注释掉了。贴出你正在测试的实际代码和实际结果,我再看一遍。
    • 感谢您的帮助,好的,我了解到“init”不是 uiviewController 的构造方法,我应该使用 initWithNibName 或 initWithCoder,这取决于我是否使用 .xib .无论如何感谢您的帮助,干杯!
    猜你喜欢
    • 2018-04-21
    • 2013-09-17
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多