3

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?

Dmitry
  • 2,837
  • 1
  • 30
  • 48
  • possible duplicate of [Use C++ with Cocoa Instead of Objective-C?](http://stackoverflow.com/questions/525609/use-c-with-cocoa-instead-of-objective-c) – Barry Wark Jun 14 '10 at 16:20

1 Answers1

4

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.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • He can actually write C++ classes in such a way that they are compatible with Objective-C classes, but keyword messages are still a problem. – jer Jun 14 '10 at 16:18
  • 3
    Why won't `objc_msgSend(view, sel_registerName("setAlpha:"), 0.4);` do what he wants? – jcanizales Nov 20 '15 at 05:36