name = [NSString stringWithString:aString];

is creating an autoreleased string. If you want to keep it around, you will need to:

[name retain];

and similarly with your height setter. I believe this will solve your problem.


Can you use or are you using Obj-C 2.0? If so, I would suggest looking into using @property & @synthesize.

You could define your height & name by doing:

@property (retain) NSNumber *height;
@property (retain) NSString *name;

and in your implementation so:

@synthesize height;
@synthesize name;

With this, you can get rid of your getter & setter code.



On Sep 29, 2008, at 4:57 PM, Adil Saleem wrote:

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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to