0

I am trying to store an object of NSObject Class using file archiving. When i tried

[NSKeyedArchiver archiveRootObject:myObject toFile:@"filePath"];

and

NSArray *myArray=@[myObject];
[NSKeyedArchiver archiveRootObject:myObject toFile:@"FilePath"];

But both returns warning !!

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ePub encodeWithCoder:]: unrecognized selector sent to instance 0xeac3570'

Karan Alangat
  • 2,154
  • 4
  • 25
  • 56

1 Answers1

1

You need to put encodeWithCoder and Decoder in your NSObject :

in your header :

- (void)encodeWithCoder:(NSCoder *)coder;
- (id)initWithCoder:(NSCoder *)coder;

in your .m

- (void)encodeWithCoder:(NSCoder *)coder;
{
    [coder encodeObject:self.yourProperty forKey:@"yourProperty"];
}



- (id)initWithCoder:(NSCoder *)coder;
{
    self = [super init];
    if (self != nil)
    {

        self.yourProperty = [coder decodeObjectForKey:@"yourProperty"];


    }
    return self;
}

you need to do that for all properties.

Note that for other types, like integers and floats, use decodeIntForKey and EncodeInt accordingly.

Neva
  • 1,330
  • 9
  • 11