views:

74

answers:

2

Hi there, i have some C code that can NOT be ported to Objective-C but it can be compiled to run on an iPhone. Now, if i want this program to have a nice GUI, how can i let it update an objc UIViewController class outlets bound to a NIB i created with interface builder? Can you please give me some (possibly full) example to understand those languages interoperability?

Thanks

NOTE: This app would be for personal use, i don't want it to be approved on the AppStore.

+2  A: 

Why not simply have your C code call a C shim that calls the appropriate Obj-C calls for the iPhone. This would be a separate library facade that acts as an isolating layer between the C code and the Obj-C.

That said, Obj-C and C are completely interoperable. They're not different like C and C++ (which has extern "C" to bind with C). Obj-C and C are, fundamentally, the same language. Obj-C has some runtime and "syntactic sugar" to facilitate its operations. But all of the structures and such are just that -- C structures and C pointers.

If you really wanted to, you can call the Obj-C runtime function to dispatch an Obj-C method. It can be done, it's just likely not worth the trouble.

I would assume that you have no problem adding C to your existing C code to access the GUI. So, I would either do the entire GUI in Obj-C and have it call your code as a library/service routine, or vice-a-versa. The former will likely be easier. Any callbacks that you code wants to make can be done call the C library shim to make the appropriate Obj-C calls.

Will Hartung
+3  A: 

Given that Objective C is a strict superset of ANSI C, not only can your C be ported to Objective C, but it already is legal Objective C.

Now, if you want to use UI framework interfaces that are in Objective C, you can create some UI interface files in C, rename them to .m files, and then call the Objective C framework interfaces directly from C functions and subroutines (and have the delegate callbacks go the other way.)

This is actually a common technique for some 3D games, which are written completely in C (mostly calls to OpenGL APIs), except for a few pages of shim classes to load an initial UIView and view controller, then the rest of the game UI is done in C (and OpenGL ES). Perfectly legal even for App store distribution.

hotpaw2
how do i create UI interfaces in C, considering that C is not C++ and therefore does not support objects?
Simone Margaritelli
Objects in Objective C are just pointers to structs in C, and your C code can just call the needed framework methods to create and handle the required UI "objects" (and follow a few other rules so as not to mess up the Obj C runtime.)
hotpaw2