0

Folks,

I'm trying to array of objects to pList by using NSCoder class. I can save some direct objects to the plist. For Ex: I've class called 'ExClass' with Sample(NSString) property. Now i could save a collection of 'ExClass' object to pList file. Now I'm trying this, but i'm getting an exception

Example:

Class FirstName
{
  NsString *first_name;
}

Class LastName
{
NsString *last_name;
}

Class Name: FirstName, LastName
{
 FirstName *first;
 LastName *last;
}

Now I would like to save a collection of name, which should have both first and last name into it. I tried by this way

.h File:

#import "Name.h"
@interface sample : NSObject <NSCoding>

@property (nonatomic, strong) Name *name;
@end

.m File:

    #import "sample.h"

    @implementation sample

    @synthesize name;

    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        if(self = [super init])    {

            self.name = [aDecoder decodeObjectForKey:@"Name"];
     }
        return self;
    }

    - (void)encodeWithCoder:(NSCoder *)encoder
    {
       self.name = [encoder encodeObject: self.first forKey:@"Name"];
    }
    @end

I got an exception, which says that, "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Name encodeWithCoder:]: unrecognized selector sent to instance".

Guys please help on this

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Which have your classes have you made conform to `NSCoding`? – Wain Apr 25 '14 at 14:21
  • Only on "Name" class. Just encoding the top class. Can you please tell me, how do i encode and decode all the data into pList. – Shaik Mohaideen Apr 25 '14 at 14:48
  • Implement it on all of your classes – Wain Apr 25 '14 at 15:09
  • 1
    @ShaikMohaideen Based on the code you posted, you have NOT added `NSCoding` to your `Name` class. You have only added to your `sample` class. You need to add `NSCoding` to your `Name` class to fix the error. – rmaddy Apr 25 '14 at 16:19

0 Answers0