I have a C function in MyClass and can call it from inside that class. Is it possible to send an instance of MyClass the C function as a message?
To explain fully, at the moment
MyClass *obj = [[MyClass alloc] init];
[obj myCFunction];
Gives the compiler error: “Receiver type ‘MyClass’ for instance message does not declare a method with selector ‘myCFunction'”. myCFunction is declared within MyClass.h @interface and the full code is before @implementation in MyClass.m and works fine if called from within the @implementation of MyClass.m. Is it therefore that C functions are private, and can I make it public so I can send it as a message to an instance of MyClass?
I am trying to write unit tests which test each stage of some calculations so simply creating an objective c method to call the c function doesn’t seem like an efficient solution.
Any help would be much appreciated
Nope. Sending messages is an Objective-C construct, so you must have a method defined for an object (or class) to send a message. You have two choices: write an instance method that calls the C function, or pass the C function the object it should use.
If the C function is declared in your header file it is public to all files that include that header file. Therefore you can call it from your testing code, it just doesn’t know what object you’re referring to if you are referencing data declared in your .m file. You should make explicit which object you are referring to in the C function by passing it a reference to that object. Within the class implementation the C function would be called with
myCFunction(self). Then access any of the object’s data through this reference rather than directly accessing the locally declared variables.