ObjC classes within OF app

really basic question… i just want to use ObjC classes within the OF app.
i must have missed something really basic…

In particular I’m trying to use an NSXMLParser class, but I can’t even manage to get NSString to work. is this possible or are all classes strictly C++?

couldn’t seem to figure it out in the http://wiki.openframeworks.cc/index.php?title=OfxiPhone-comprehensive-guide

hi, you can either rename your .cpp to .mm, then the compiler will treat it as Obj-C++. Or you can right click on the file and from it’s properties specific it as Obj-C++ (I prefer the first method of renaming, so it’s clear at a glance which files will have a mixture of Obj-C and C++)

that did it, much thanks memo!

Can you program an OF app in Objective-C (which I like) rather than C++, by which I mean using the Objective-C language as well as classes? I’m aware of Objective-C++, but I’ve not been able to compile an OC++ app yet.

Or is OF not that compatible?

interesting question. haha

I think it would be difficult.
as far as I have discovered, accessing objective-c object properties from within c++ doesn’t always work, you need to create accessor functions for them. More importantly though, since c++ classes cannot act as objective-c delegates, every time you need this to happen, which is often in obj-c, you need to write an objective c class that will communicate with your c++ one…

but if you’re not using delegates, and synthesizing all of your properties, it should work great. just make sure you change all your .cpp’s to .mm’s

if you’re going the other route, just instantiating the c++ classes from obj-c, that might work… but i really dont have any idea about that

Hey Anny :slight_smile:

using Obj-C classes and using the Obj-C language is kinda the same thing. The Obj-C language is merely C with a few special statements (which begin with @), and the objects/classes that come with it (NSObject etc.). So while your testApp is a C++ class, and ofImage, ofTexture etc are all C++ classes, every single bit of code you write can be Objective-C, as long as you use the C++ classes in the C++ way (i.e. you can’t say ofImage* myImage = [[ofImage alloc] init];

So are you saying that something like this might work (made up quickly, will not actually compile):

@implementation objc_class  
@synthesize objc_class_property;  
 * Objective-C style function  
-(void) local_method: (NSObject*) arg  
  // Do stuff here  
 * C++ function inherited from ofxMultiTouch  
void objc_class::touchDown( float x, float y, int touchId, ofxMultiTouchCustomData *data )  
  // Do stuff here  


nope, you can’t mix actual classes. An obj-c class can’t extend an c++ class or vice versa they are completely different entities. Basically all the classes that you create for your app, can be objective c. And in the testApp::callbacks (setup, update, draw, touchdown etc.) you can write objective-c code to send messages to them. and in your objective-c classes you can call c++ methods.

(written in browser, may not compile)

@interface objc_class  
 ofImage* myImage;  
@implementation objc_class  
-(void)loadImage:(NSString)filename {  
   ofImage.loadImage([filename utfString]);  
-(void)update {  
/// blah  
-(void)draw {  
/// blah  
class testApp {   
   NSMutableArray *objects;  
testApp::setup() {  
   objects = [[NSMutableArray alloc] init];  
   id tempObj = [[objc_class alloc] init];  
   [myObjCObject loadImage:@"blah.png"];  
   [objects addObject:tempObj];  
   [tempObj release];  
testApp::update() {  
   [objects makeObjectsPerformSelector:@selector(update)];  
testApp::draw() {  
   [objects makeObjectsPerformSelector:@selector(draw)];  
testApp::touchDown(x,y,...) {  
   for(id object in objects) {  

so as you can see, very little C++ code here. all objective c inside the C++ skeleton. hope that helps!

Okay so basically structures (classes etc) must be commit to either C++ or Objective-C format, but any procedural code can be either as long as it appropriates the correct language on an object by object basis?

exactly, you can either create a class (c++) or a @class (obj-c). You can mix apples and oranges. But you can’t make cider out of an orange.

Ok, I’m a little confused…and a newbie

I have an Objective-C-class for controlling and disabling some features on a UVC camera, which works great, but now I want to call it from OF.
I’ve changed my testApp.cpp and testApp.h files to .mm and added the the class to my project.

Here’s a little code, as you can see it’s a mix of Obj C and C++: :?

void testApp::setup(){	   
camWidth 		= 640;  
camHeight 		= 480;  
videoInverted 	= new unsigned char[camWidth*camHeight*3];  
videoTexture.allocate(camWidth,camHeight, GL_RGB);  
cameraControl = [[UVCCameraControl alloc] initWithVendorID:0x046d productID:0x09A6];  
// connects to the Logitech QuickCam Vision Pro  
[cameraControl setAutoExposure:NO];  
[cameraControl setExposure:0.7];  

It compiles ok and everything seems gravy until I try to set some of the properties within the class (i.e the last two lines of code)…I get an error:

Unable to open interface (e00002c5)

Is this because I need an accessor function to communicate to the class? How do I go about writng one?

Thank you for any help you can give me.

I think your issue might be relative to the class. It’s hard to debug this kind of stuff without looking at the code.

everything you’ve done so far looks right. I guess maybe just make sure you’re including all of the appropriate frameworks and header files? It should just work

Did somebody get the UVC control to work for mac in OF?

it seems to be that the initGrabber and uvc control intervier with each other.

commanded all the videoGrabber related lines out and now i don’t get error messages when changing the cameracontrol values.
it means though i don’t have an image, due to commenting all videograbber stuff out.

maybe we should continue having this conversation in this thread
“UVC Camera control in Mac OS X”