I am attempting a method swizzle in Obj-C but I would like to pass it a pure C function. This means I need to somehow assign a selector and/or manually build an objc_method
struct. Maybe somehow leverage NSInvocation
?
My understanding is that due to the fact that Obj-C is a strict superset of C and therefor fully compatible.
What I have going now:
main.m :
#include....
CFStringRef strRet(void) {
return CFSTR("retString");
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
SEL _strRet = sel_registerName("strRet");
//I also tried: SEL _strRet = NSSelectorFromString(@"strRet");
Class bundle = objc_getClass("NSBundle");
method_exchangeImplementations(
class_getInstanceMethod(bundle, sel_registerName("anySelector")),
class_getInstanceMethod(bundle, sel_registerName("_strRet")
);
I have tried putting the C function inside @implementation
(which I would like to avoid) and even then it did not work.