Re: Saving NSArray of custom objects
Re: Saving NSArray of custom objects
- Subject: Re: Saving NSArray of custom objects
- From: Jonathan Hess <email@hidden>
- Date: Mon, 13 Jul 2009 17:29:57 -0700
Hey dkj -
The method -[NSArray writeToFile: atomically:] uses property list
serialization, and property lists support a fixed set of types. You
want to serialize your NSArray with an NSCoder, like NSKeyedArchiver,
and then read it back in with NSKeyedUnarchiver.
Take a look at these two methods:
-[NSKeyedArchiver archiveRootObject:toFile:]
-[NSKeyedUnarchiver unarchiveObjectWithFile:]
Good luck -
Jon Hess
On Jul 13, 2009, at 5:22 PM, DKJ wrote:
I've defined my own MyClass and made it conform to the NSCoding
protocol by adding methods like this:
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeInt:index forKey:@"index"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
index = [decoder decodeIntForKey:@"index"];
return self;
}
I then put a bunch of these into an NSArray and try to save the
array like this:
NSString *path = [NSHomeDirectory()
stringByAppendingPathComponent:FILE_PATH];
BOOL didIt = [theArray writeToFile:path atomically:NO];
But nothing is saved: didIt = 0.
I must be missing a step somewhere. I've been looking at "Archives
and Serializations Programming Guide for Cocoa", and finding it
rather bewildering. A nudge in the right direction would be much
appreciated.
dkj
_______________________________________________
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
_______________________________________________
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