NSMutableArray insertObject:(id)atIndex:int插入void ptr

Ami*_*k12 2 c++ cocoa objective-c++ nsmutablearray

在我的应用程序中,我有一个C++和Objective C++代码的混合,在一个地方,我需要插入c ++类对象的指针到NSMutableArray,但我得到NSINvalidArgument异常,任何人都可以指导我,我怎么能插入无效指针NSMuatableArray

这是我试过的,

pArray = [[NSMutableArray alloc]initWithCapacity:myList.size()];

Node *node = myList.getHead();

int idx =0;
void *ptr = nil;
while ( node ) {
    [pCTArray insertObject:(NSObject *)node atIndex:idx];
    node = node->getNext();
    idx++;

}
Run Code Online (Sandbox Code Playgroud)

是否有任何其他方法将其插入MutableArray,我做的可能的解决方法是:有商店索引链接

[myArray insertObject:[NSNumber numberWithInt:idx] atIndex:idx];
Run Code Online (Sandbox Code Playgroud)

我将在所有NSTable/NSOutliveVIew委托方法中使用此数组,我需要选择索引并从链接列表中获取元素,但由于性能而担心,因为需要太多的函数调用,

这是Cocoa中的任何错误,或者我做错了什么?

cob*_*bal 5

NSArrays期望保持符合NSObject协议的Objective-C对象.如果要包装指针,请使用NSValue:

[myArray insertObject:[NSValue valueWithPointer:node] atIndex:idx];
Run Code Online (Sandbox Code Playgroud)

您手动负责该指针的所有内存管理,因为Objective-C无法以通常的方式引用它.