NSKeyedArchiver 和描述方法:应用程序崩溃
我尝试测试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的应用程序正在崩溃,因为这一行是错误的:
age 是一个 int,而不是一个对象指针。需要:
我认为您的编码/解码代码没有任何明显的错误。
Your app is crashing because this line is wrong:
age is an int, not an object pointer. Needs to be:
I don't think there's anything obviously wrong with your encode/decode code.