views:

127

answers:

2

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?

+3  A: 

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
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
A: 

Hye,

I would like to mix C++ and objective-C but I can't understand why there is so mush errors. In this example I tried to put Objective-C UIView type in an C++ class.

Environnement.h

ifndef ENVIRONNEMENT_H

define ENVIRONNEMENT_H

class Environnement{ private : Environnement(); ~Environnement(); protected: static Environnement* instance;

public: static const int WIDTH = 1024; static const int HEIGHT = 768;

static Environnement* getInstance();
static void freeEnvironnement();



UIView* getScene();

};

endif

Environnement.mm

include "Environnement.h"

import

import

UIView* canvas = nil;

Environnement* Environnement::instance = 0;

Environnement::Environnement(){ canvas = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Environnement::WIDTH, Environnement::HEIGHT)];

}

Environnement::~Environnement(){ [canvas release]; }

Environnement* Environnement::getInstance(){ if(instance){ instance = new Environnement(); }

return instance;

}

void Environnement::addAgent(Agent* ag) const{ //[canvas addSubview:ag->getBodyInterface()]; }

void Environnement::freeEnvironnement(){ delete instance; }

UIView* Environnement::getScene(){ return canvas; }

I tried several solutions in order to put UIView* in the *.h file but at each time I have a thousand of errors. Why? Can anyone have a solution?

nosri