-2
NSCoder *coder; 
coder=[[NSCoder alloc]init];
[coder encodeObject:@"value" forKey:@"frame"];

Error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -encodeObject:forKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!`


this breaks during run time.can any body help me to find it what i am doing wrong?

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
  • Unless you tell us what happens exactly, no, we cannot help. – Andy Ibanez Dec 19 '16 at 02:19
  • erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -encodeObject:forKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!' – Manzoor Husain Dec 19 '16 at 02:26
  • 2
    Reading the first alinea of the documentation of `NSCoder` might help. – Willeke Dec 19 '16 at 02:55
  • As per the error message, `NSCoder` is not meant to be used directly; instead, use a concrete subclass like `NSKeyedArchiver` to do your archival. – Itai Ferber Dec 19 '16 at 04:48

1 Answers1

-2

Need to create a model class, and in this model class, need to implement a NSCodingProtocol

e.g Book Class

Book.h

@interface Book : NSObject<NSCoding>

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *author;

@end

Book.m

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.title = [aDecoder decodeObjectForKey:@"name"];
        self.author = [aDecoder decodeObjectForKey:@author"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_name forKey:@"name"];
    [aCoder encodeObject:_author forKey:@"author"];
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Rishi
  • 167
  • 1
  • 7
  • This has nothing to do with the author's problem. You also don't necessarily need a model class in order to encode data; you can encode whatever you want at the top level. – Itai Ferber Dec 19 '16 at 06:35