• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem in using NSKeyedUnarchiver for my custom class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem in using NSKeyedUnarchiver for my custom class


  • Subject: Re: Problem in using NSKeyedUnarchiver for my custom class
  • From: Eric Gorr <email@hidden>
  • Date: Mon, 29 Sep 2008 17:25:29 -0400

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@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


References: 
 >Problem in using NSKeyedUnarchiver for my custom class (From: Adil Saleem <email@hidden>)

  • Prev by Date: Problem in using NSKeyedUnarchiver for my custom class
  • Next by Date: Re: More on Keyboard Event posting
  • Previous by thread: Problem in using NSKeyedUnarchiver for my custom class
  • Next by thread: Passing focus to a control
  • Index(es):
    • Date
    • Thread