Skip to content

Instantly share code, notes, and snippets.

@k06a
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k06a/3e8703538d9c4063cf04 to your computer and use it in GitHub Desktop.
Save k06a/3e8703538d9c4063cf04 to your computer and use it in GitHub Desktop.
#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: http://www.mikeash.com/pyblog/custom-nscells-done-right.html
- (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;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment