Skip to content

Instantly share code, notes, and snippets.

@grp
Forked from drunknbass/gist:3074015
Created July 9, 2012 04:40
Show Gist options
  • Save grp/3074235 to your computer and use it in GitHub Desktop.
Save grp/3074235 to your computer and use it in GitHub Desktop.
Swizzletastic
#include <objc/runtime.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SwizzleMyNizzleProtocol.h"
IMP SwizzleMethod(Class _class, SEL sel, IMP imp) {
if (_class == nil || sel == NULL || imp == NULL)
return NULL;
Method method = class_getInstanceMethod(_class, sel);
if (method == nil)
return NULL;
unsigned int count;
Method *methods = class_copyMethodList(_class, &count);
for (unsigned int index = 0; index < count; index++)
if (methods[index] == method) {
free(methods);
return method_setImplementation(method, imp);
}
}
free(methods);
class_addMethod(_class, sel, imp, method_getTypeEncoding(method));
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment