Problem in using NSKeyedUnarchiver for my custom class
Problem in using NSKeyedUnarchiver for my custom class
- Subject: Problem in using NSKeyedUnarchiver for my custom class
- From: Adil Saleem <email@hidden>
- Date: Mon, 29 Sep 2008 13:57:19 -0700 (PDT)
Hi,
I am trying to write instances of a class (subclass of NSObject) in an array and then write them to a preferences files using NSKeyedArchiver. It does so successfully. After i unarchive it using NSKeyedUnarchiver, it reads the contents in the array. It also reports correct no. of objects of that class in the array. But when i try to use those objects, program crashes. To check the program, i did this with simple NSArray instances instead of my class objects. It worked fine. So i have my doubts on my class. Any ideas why i have done wrong in my class. Here is the code of my class.
data.h
@interface data : NSObject <NSCoding>{
NSNumber *height;
NSString *name;
}
-(NSNumber *)height;
-(void)setHeight:(NSNumber *)aNumber;
-(NSString *)name;
-(void)setName:(NSString *)aString;
-(void)initWithParams:(float)aFloat name:(NSString *)aString;
-(void)dealloc;
-(id)initWithCoder:(NSCoder*)coder;
-(void)encodeWithCoder:(NSCoder*)coder;
data.m
@implementation data
-(void)initWithParams:(float)aFloat name:(NSString *)aString
{
[super init];
height = [NSNumber numberWithFloat:aFloat];
name = [NSString stringWithString:aString];
}
-(void)setHeight:(NSNumber *)aNumber
{
if(height != aNumber)
{
[height autorelease];
height = [NSNumber numberWithFloat:[aNumber floatValue]];
}
}
-(NSNumber *)height
{
return height;
}
-(void)setName:(NSString *)aString
{
if(name != aString)
{
[name autorelease];
name = [NSString stringWithString:aString];
}
}
-(NSString *)name
{
return name;
}
-(id)initWithCoder:(NSCoder*)coder
{
if(self = [super init])
{
[self setHeight:[coder decodeObjectForKey:@"Height"]];
[self setName:[coder decodeObjectForKey:@"Preset Name"]];
}
return self;
}
-(void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:height forKey:@"Height"];
[coder encodeObject:name forKey:@"Preset Name"];
}
-(void)dealloc
{
[super dealloc];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden