Problem with archiving Object

Discussion in 'Mac Programming' started by sujithkrishnan, May 16, 2008.

  1. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    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....
     
  2. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #2
    Please post your initWithCoder: and encodeWithCoder: methods.
     
  3. mysticwhiskey macrumors newbie

    Joined:
    Mar 31, 2008
    #3
    Does your class (the one that myBookObject is an instance of) implement the NSCoding protocol?

    http://www.cocoadev.com/index.pl?NSCoding
     
  4. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #4
    Yeah... i tried it with implementing <NSCoding> protocol...
    Still the file is empty , But the object is having all cvalues when i call the archiving...
    Is it mandatory to implement initWithcoder and encodeWithCoder?
     
  5. Sijmen macrumors 6502a

    Joined:
    Sep 7, 2005
    #5
    Yes.

    In those methods you do the actual archiving and unarchiving, using the supplied archiver object.
     
  6. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #6
    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..
     
  7. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #7
    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???
     
  8. Sijmen macrumors 6502a

    Joined:
    Sep 7, 2005
    #8
    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"];
    }
     
  9. dgdosen macrumors 65816

    dgdosen

    Joined:
    Dec 13, 2003
    Location:
    Seattle
    #9
    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!
     

Share This Page