Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using UIKit from C/C++

The questions might have been already asked, but I want to understand if iPhone's frameworks limited only to Objective-C use or it could also be accessed from C/C++ code?

For example, is it possible to use standard iPhone controls from C/C++ code? Are standard classes like NSArray, NSSet accessible from C/C++ code?

If they are, could any one give a link to some examples how to do it?

like image 978
Dmitry Avatar asked Jun 25 '26 16:06

Dmitry


1 Answers

You can mix Objective C with C++. Just rename your source file to .mm.

Some Foundation classes such as NSArray and NSSet are toll-free bridges to CoreFoundation objects like CFArray and CFSet. The latter is a pure C library that you can use without ObjC.


UIKit is a pure Objective-C framework. You could use UIKit with C and C++ as the Objective-C runtime is accessible from pure C functions such as objc_msgSend, e.g.

id UIView = objc_getClass("UIView");
SEL alloc = sel_registerName("alloc");
id view = objc_msgSend(UIView, alloc);
// ...

but it is not reliable, e.g.

objc_msgSend(view, sel_registerName("setAlpha:"), 0.4);

will not do what you want.

You could isolate the UI part and write ObjC just for that part. Create a C interface between the UI and the backend, which you may use C++ or any (allowed) languages.

like image 134
kennytm Avatar answered Jun 27 '26 04:06

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!