Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
@implementation NSControl (Subclasses)
+ (void)load
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
method_exchangeImplementations(class_getInstanceMethod([self class], @selector(initWithCoder:)),
class_getInstanceMethod([self class], @selector(initWithCoder2:)));
// Inspired from:
- (id)initWithCoder2:(NSCoder *)coder
if (![coder isKindOfClass:[NSKeyedUnarchiver class]])
return [self initWithCoder2:coder];
NSKeyedUnarchiver * unarchiver = (NSKeyedUnarchiver *)coder;
Class rootclass = [self superclass];
while ([NSStringFromClass(rootclass) rangeOfString:@"NS"].location == NSNotFound)
rootclass = [rootclass superclass];
Class supercell = [rootclass cellClass];
Class selfcell = [[self class] cellClass];
if (!selfcell || !supercell)
return [self initWithCoder2:coder];
NSString * supercellName = NSStringFromClass(supercell);
[unarchiver setClass:selfcell forClassName:supercellName];
self = [self initWithCoder2:coder];
[unarchiver setClass:supercell forClassName:supercellName];
return self;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment