简体   繁体   English

将C ++对象添加到Objective C集合(NSSet)时遇到问题

[英]Trouble with adding C++ objects to Objective C collections (NSSet)

I'm busy implementing ZXing with a QRCodeReader into my project. 我正在将QRCodeReader的ZXing实施到我的项目中。 QRCodeReader is mainly C++ and my project objective-C. QRCodeReader主要是C ++,而我的项目目标是C。 I have managed to implement it properly so I can use the QRCodeReader objects into my objective-C implementation (.mm file). 我已经设法正确实现它,因此可以将QRCodeReader对象用于我的Objective-C实现(.mm文件)。 But now I need to pass this C++ object to the zxWidController.reader property. 但是现在我需要将此C ++对象传递给zxWidController.reader属性。

This means I will have to set the C++ object into an NSSet Object. 这意味着我将不得不将C ++对象设置为NSSet对象。

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. 我将C ++对象转换为(id),现在可以正确编译了。 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? 还有其他/更好的方法可以实现我的目标吗?

A C++ type is not an Objective C type. C ++类型不是Objective C类型。 You cannot send it messages, in particular retain and release, which NSSet does. 您不能向其发送消息,尤其是保留和释放,NSSet会这样做。 There's an Objective C type of the same name that you want to use. 您要使用同名的Objective C类型。 (I'll update my other answer). (我将更新其他答案)。

The code above does the trick. 上面的代码可以解决问题。 I casted the C++ object to (id) and now it compiles properly. 我将C ++对象转换为(id),现在可以正确编译了。 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. 不能您不能通过将任意指针转换为id来使其成为有效的Objective-C对象指针。

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. 重新定义您的zxWidController类以采用指向C ++对象而不是Obj-C对象的指针。

  • Wrap qrcodeReader in a NSValue. 将qrcodeReader包装在NSValue中。

  • Take a look at NSPointerArray as a replacement for NSSet. 看一下NSPointerArray作为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. 次要nitpick:暂时忘记qrcodeReader指向C ++对象这一事实,创建一个空集只是为了可以向其添加对象,这似乎很愚蠢。 If you were going to use an Obj-C set, then either create it using +setWithObject: or use NSMutableSet instead. 如果要使用Obj-C集,请使用+ setWithObject:创建它,或者改用NSMutableSet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM