I’m using encoding for the first time in order to save data out of my app. The examples I have been looking at say to call [super encodeWithCoder:coder]; for custom classes.
I have a class which extends NSObject but am getting the error msg below. Is this because NSObject is the base class and has nothing to encode?
Receiver type ‘NSObject’ for instance message does not declare a method with selector ‘encodeWithCoder:’
- (void)encodeWithCoder:(NSCoder *) coder
{
[super encodeWithCoder:coder]; // <-------------------------ERROR
[coder encodeInt:noteIndex forKey:@"noteIndex"];
[coder encodeCGRect:cellRect forKey:@"cellRect"];
[coder encodeBool:noteOn forKey: @"noteOn"];
[coder encodeInt:note forKey:@"note"];
[coder encodeInt:velocity forKey:@"velocity"];
[coder encodeInt:channel forKey:@"channel"];
[coder encodeInt:releaseVelocity forKey:@"releaseVelocity"];
[coder encodeFloat:duration forKey:@"duration"];
}
That’s right,
NSObjectdoesn’t conform toNSCoding, so it doesn’t implementencodeWithCoder:. Luckily,NSObjectitself doesn’t have any data to encode, so all you have to do is:And your
initWithCoder:would just look like: