Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Hi all..

In my apps, i am having a book class with isbn,author,title, price all as data members.

I used properties for all to assign values. And i archived using code like below

result = [NSKeyedArchiver archiveRootObject:myBookObject
toFile:mad:"/tmp/MapArchive"];

and the file is created at specified path, and also the myBookObject is having all the values for isbn,title,author and price...
But the object is not getting archived, means i cant find the values or fields in that file, which created...

Help me ASAP....
 

mysticwhiskey

macrumors newbie
Mar 31, 2008
25
0
Hi all..

In my apps, i am having a book class with isbn,author,title, price all as data members.

I used properties for all to assign values. And i archived using code like below

result = [NSKeyedArchiver archiveRootObject:myBookObject
toFile:mad:"/tmp/MapArchive"];

and the file is created at specified path, and also the myBookObject is having all the values for isbn,title,author and price...
But the object is not getting archived, means i cant find the values or fields in that file, which created...

Help me ASAP....

Does your class (the one that myBookObject is an instance of) implement the NSCoding protocol?

http://www.cocoadev.com/index.pl?NSCoding
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Yes.

In those methods you do the actual archiving and unarchiving, using the supplied archiver object.

Okie...
I will try that...
My one more doubt is that among archiving and serialization wch is the actual Object persistance approach??
Whether srlzation can store object physically?? I mean i got a little confused wt both approach..I am not having much programming exprnce in this field.. All what i am asking based on the doubts from what i read.
Thanks..
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Yes.

In those methods you do the actual archiving and unarchiving, using the supplied archiver object.

I will clarify my question a little more

I am having a book class. In my main class i am archiving an object of book class. I implemented NSCoding protocol for book class. My question is if i am writing the implementation of initWithCoder and encode WithCoder methods in book class, what its body must be?? How can i call archiving in that method as i want to archive its object which is created in main class???
 

Sijmen

macrumors 6502a
Sep 7, 2005
709
1
When you encode an object, the archiver will make sure that encodeWithCoder: will be called. The same deal the other way around – when you load an object form an archiver, the object will be recreated with initWithCoder.

Inside the methods, you can get and set the values the archiver has to save using encode*ForKey: and decode*ForKey:. By the way, you can have as much nested objects as you want.

Here is some example code from one of my projects:

Code:
- (id)initWithCoder:(NSCoder *)decoder
{
	self = [super init];
	if (self)
	{
		time = [decoder decodeFloatForKey:@"time"];
		length = [decoder decodeFloatForKey:@"length"];
		angle = [decoder decodeFloatForKey:@"angle"];
	}
	
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	[coder encodeFloat:time forKey:@"time"];
	[coder encodeFloat:length forKey:@"length"];
	[coder encodeFloat:angle forKey:@"angle"];
}
 

dgdosen

macrumors 68030
Dec 13, 2003
2,817
1,463
Seattle
I'm having a bit of a problem with Archiving nested objects...

Here's my scenario:

I have three layers of my own classes -

To keep it simple (although it isn't). Each of these nested objects derives from <NSCoding>

LayerA
-NSString a1
-LayerB a2

LayerB
-NSMutableArray (b1) of LayerC

LayerC
-NSString c1
-NSString c2
...


I get an error during the encoding process -
It seems I can move down the encoding process - and hit all my calls into encodeWithCoder at each layer - the debugger starts moving into code I don't have symbols for. I'm therefore not sure if the error happens there.

The app will finish, but when I open it back up and try to decode, I'll get this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[XXX copyWithZone:]: unrecognized selector sent to instance 0x450370'


Any ideas of what I'm doing wrong? I feel like I need to do more to get the nested objects into the archive...

By the way, If I just do a "shallow" archive - it works like a champ... The problem is I want to do a "deep" archive...

Thanks in advance!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.