Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure C function calling Objective-C method?

Okay, I've read half a dozen threads on this subject, but none of the solutions appear to address exact needs.

Question:

How does a Pure C (.c) function call a method inside a Pure Objective-C (.m) class?

Every example / answer is using C inside an Objective-C (.m) method. I have a Pure C library that I have to create a simulator for, so I need to keep my Kernel in pure C and call out to my higher level emulation methods in Objective-C.

Any attempt I make to create a reference and call a method fails. Square bracket notation fails as well. Creating a global var in Obj-C and trying to use that in Pure-C doesn't work, as if the namespace is segregated.

Anyone done this?

Here's a diagram of the flow:

Obj-C UIButton CLICKED->Calls Obj-C method->Calls C function->Call Obj-C method

like image 825
Mark Löwe Avatar asked Dec 06 '13 08:12

Mark Löwe


People also ask

How do you call a function in Objective-C?

An Objective-C function is declared using the following syntax: <return type> <function name> (<arg1 type> <arg1 name>, <arg2 type> <arg2 name>, ... ) Explanations of the various fields of the function declaration are as follows: <return type> - Specifies the data type of the result returned by the function.

Can you use C in Objective-C?

“Using C in Objective-C” is somewhat misleading, because Objective-C is a strict superset of the C language. You really can't use C in Objective-C, since Objective-C is C.

What does @() mean in Objective-C?

In Objective-C, any character , numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C's type suffixes may be used to control the size of numeric literals. '@' is used a lot in the objective-C world.

Can I call Objective-C from C++?

You can intermix C++ and Objective-C code to some degree. To instruct the compiler that a file contains C++ code as well as Objective-C, use the file extension . mm or . M instead of .


2 Answers

After much experimenting, I found that the most elegant way of solving my problem was to turn my core C library into an NSObject using the .m suffix. The method of calling back and forth resolved instantly. This change DOES alter my original library, but by so little, it's manageable. So to review:

My original C file was renamed to use the .m suffix. Then I added

@interface myCLibrary : NSObject

@end

to my .h file, and added to my formerly .c file, now renamed .m.

@implementation myCLibrary

@end

Just remember that C functions aren't to be pasted between these interface / implementation declarations, below them. Only Objective-C is to go inside these statements. Once I did that, calling the C functions, and calling BACK to other C functions worked great.

Thanks for all the help regardless.

like image 136
Mark Löwe Avatar answered Sep 27 '22 17:09

Mark Löwe


Objective C can compile c method without any modification. To call c method from objective-c class you have to whatever you do in c, just include the header file then call method directly. Suppose you have a C header named test.h and in that you have a method sum(int i, int j); then first include test.h and then call test(1, 2);

If you want to call C++ method, use Objective-C++ (.mm extension) in the same manner as explained above.

like image 43
Abdullah Md. Zubair Avatar answered Sep 27 '22 16:09

Abdullah Md. Zubair