It is possible to declare test_func
in a special way and use various Objective-C runtime API functions to “attach” that method implementation to a class's method list, but the simplest way to do this:
testfunc.h
#ifndef TESTFUNC_H
#define TESTFUNC_H
int test_func();
#endif
testfunc.c
#include "testfunc.h"
int test_func()
{
return 4;
}
TestClass.h
#import <Foundation/Foundation.h>
@interface TestClass : NSObject
- (int) test_func;
@end
testclass.m
#import "TestClass.h"
#import "testfunc.h"
@implementation TestClass
- (int) test_func
{
return test_func();
}
@end
If you are still keen to try adding methods at runtime, have a look at these links:
Dynamic runtime resolution
Using runtime API method class_addMethod
It is worth noting that for anything as trivial as calling a C function you should avoid dynamic method injection/resolution for the sake of maintainability and readability. Unless you have an ulterior motive that you haven't explained in your question, stick with Plan A (the simple route)!