Swizzle class property?


Shane Stanley
 

The subject more or less says it. I tried using code similar to how I do it with instance methods, to no avail. I'm now wondering if it's actually possible. (It's a read-only property, if that makes a difference.)

+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class theClass = [self class];

SEL originalSelector = @selector(classPropertyName); // name of original property
SEL swizzledSelector = @selector(patchedClassPropertyName); // new method

Method originalMethod = class_getClassMethod(theClass, originalSelector);
Method swizzledMethod = class_getClassMethod(theClass, swizzledSelector);

BOOL didAddMethod =
class_addMethod(theClass,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
class_replaceMethod(theClass,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}



--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Join cocoa@apple-dev.groups.io to automatically receive all group messages.