I'm trying to create a generic implementation for the NSCoding protocol. The code will be wrapped around in a macro the will implement NSCoding. In order to implement the protocol, we need two functions:
-(void)encodeWithCoder:(NSCoder*)coder;
-(id)initWithCoder:(NSCoder*)coder;
A generic implementation of the initWithCoder function would be:
-(id)initWithCoder:(NSCoder*)coder {
if ([super conformsToProtocol:@protocol(NSCoding)])
self = [super initWithCoder:coder];
else {
self = [super init];
}
if (!self) return self;
self = [MyGenericCoder initWithCoder:coder forObject:self withClass:[__clazz class]];
return self;
}
The problematic line is self = [super initWithCoder:coder];
it will not compile since super does not respond to initWithCoder:
when we use the in a class that its super does not implements NSCoding. Casting super to NSObject<NSCoding>*
will not work with the LLVM compiler.
[super performSelector:(initWithCoder:) withObject:coder]
will not work either since super == self, Which will result in an infinite loop.
How can I call [super initWithCoder:coder]
in manner that will trigger the function in the superclass and will not generate a compilation warning / error?