Question

I'm busy implementing ZXing with a QRCodeReader into my project. QRCodeReader is mainly C++ and my project objective-C. I have managed to implement it properly so I can use the QRCodeReader objects into my objective-C implementation (.mm file). But now I need to pass this C++ object to the zxWidController.reader property.

This means I will have to set the C++ object into an NSSet Object.

QRCodeReader* qrcodeReader = new QRCodeReader();
NSSet *readers = [[NSSet alloc ] init];
[readers setByAddingObject:(id)qrcodeReader];
widController.readers = readers;
[readers release];

The code above does the trick. I casted the C++ object to (id) and now it compiles properly. But is this the proper way to do it?

Is this manner of programming the proper way to do this?

Are there any other / better ways to achieve my goal?

Was it helpful?

Solution

A C++ type is not an Objective C type. You cannot send it messages, in particular retain and release, which NSSet does. There's an Objective C type of the same name that you want to use. (I'll update my other answer).

OTHER TIPS

The code above does the trick. I casted the C++ object to (id) and now it compiles properly. But is this the proper way to do it?

No. You can't just make an arbitrary pointer into a valid Objective-C object pointer by casting it to id.

Is this manner of programming the proper way to do this?

Again, no.

Are there any other / better ways to achieve my goal?

You can try any of the following:

  • Redefine your zxWidController class to take a pointer to a C++ object instead of an Obj-C object.

  • Wrap qrcodeReader in a NSValue.

  • Take a look at NSPointerArray as a replacement for NSSet. Definitely read the docs before you try to use it -- you have to configure it's memory management policies to do the right thing for the type of pointer you're storing.

Minor nitpick: Forgetting for a moment about the fact that qrcodeReader points to a C++ object, it seems silly to create an empty set just so that you can add an object to it. If you were going to use an Obj-C set, then either create it using +setWithObject: or use NSMutableSet instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top