简体   繁体   English

在 NSMutableArray 中添加 c++ object

[英]Adding c++ object in the NSMutableArray

I have MeetingUser C++ class;我有会议用户 C++ class; new_meetingUser is its object. new_meetingUser 是它的 object。

I want to add it to the NSMutableArray meetingUsersList, which I did using NSValue.我想将它添加到我使用 NSValue 所做的 NSMutableArray meetingUsersList。 However, I can't make out how to handle the memory of the object and the ownership of NSMutableArray.但是,我不知道如何处理 object 的 memory 和 NSMutableArray 的所有权。

MeetingUser *new_meetingUser = new MeetingUser(userName, dispName );
[meetingUsersList addObject:[NSValue valueWithPointer:new_meetingUser]];

You can add the C++ object to the array like that, but the array will not take ownership of the object. 您可以像这样将C ++对象添加到数组,但是该数组不会获得该对象的所有权。 You will have to ensure that the object is not deleted while it's in the array, otherwise it will become a dangling pointer. 您必须确保对象在数组中时不会被删除,否则它将成为悬空指针。 Then you have to delete the object manually later, when you know it is safe to do so. 然后,如果您知道可以安全地删除对象,则必须稍后手动删除它。

If you want the array to take ownership of the object, then you will have to use a CFMutableArray . 如果要让数组获得对象的所有权,则必须使用CFMutableArray CFMutableArray allows you to specify functions that called when objects are removed from the array, or when the array is deallocated. CFMutableArray允许您指定从数组中删除对象或释放数组时调用的函数。 In your case, you want to set the CFMutableArray to call delete on the objects. 在您的情况下,您想要设置CFMutableArray以在对象上调用delete

Essentially, you can't. 本质上,您不能。 C++ objects and Objective-C objects are totally different things, despite both being called "objects." C ++对象和Objective-C对象完全不同,尽管它们都被称为“对象”。 NSArray can only hold Objective-C objects. NSArray只能保存Objective-C对象。 It can't hold primitives, structs, C++ objects or anything else that is not an Objective-C object. 它不能保存原语,结构,C ++对象或非Objective-C对象的任何其他对象。 The best workaround is to create an Objective-C class that wraps your C++ class and store that in the array. 最好的解决方法是创建一个Objective-C类,包装你的C ++类和商店在数组中。

One approach would be to write a small ObjC class to wrap your C++ object. 一种方法是编写一个小的ObjC类来包装C ++对象。 This can be as simple or as complex as you want -- the only major thing it needs to do is store the C++ object in a field, and delete it in dealloc . 这可以根据需要简单或复杂-它唯一要做的主要事情是将C ++对象存储在字段中,然后deletedeletedealloc

Similar problem I had我有类似的问题

Create Objective C Wrapper class MeshHolder.hh创建目标 C 包装器 class MeshHolder.hh

#import <Foundation/Foundation.h>


#include "Sculpt.h"

NS_ASSUME_NONNULL_BEGIN

@interface MeshHolder : NSObject

-(void) setupMesh:(Mesh_CPP *)mesh;
-(Mesh_CPP *)getMesh;

@end

NS_ASSUME_NONNULL_END

and.MM file和.MM文件

#import "MeshHolder.hh"



@implementation MeshHolder

Mesh_CPP * meshTEMP_;


-(void) setupMesh:(Mesh_CPP *)mesh {
    meshTEMP_ = mesh;
}

-(Mesh_CPP *)getMesh {
    return meshTEMP_;
}

@end

How to use it如何使用它

@property(strong,nonatomic) NSMutableArray<MeshHolder*>  *arrMeshes;

 MeshHolder *meshH = [MeshHolder new];
 [meshH setupMesh:meshTemp];
 [_arrMeshes addObject:meshH];

Hope it is helpful as a quick example希望它作为一个简单的例子有帮助

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

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