开发者

NSMutableArray insertObject:(id) atIndex:int inserting a void ptr

In my application, i have a mix of C++ and Objective C++ code, at one place, i need to insert pointer of c++ class object to NSMutableArray , but i am getting NSINvalidArgument exception, Can anyone guide me, how can i insert void pointer to NSMuatableArray

This is what i have tried ,

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++;

}

Is there any otherway to insert it into the MutableArray, the possible workaround i made is : having store index link this

[myArray insertObject:[NSNumber numberWithInt:idx] atIndex:idx];

and this array i would be using in all NSTable/NSOutliveVIew delegate method , i need to pick the index and get the elemen开发者_开发技巧t from the linklist, but worried because of performance, as too many function call would be needed,

Is this any bug in Cocoa or i am making anything wrong ?


NSArrays expect to hold objective-C objects conforming to the NSObject protocol. If you wish to wrap a pointer, use NSValue:

[myArray insertObject:[NSValue valueWithPointer:node] atIndex:idx];

You are manually responsible for all memory management of that pointer, as objective-C can't reference count it in the usual way.


NSMurableArray only accepts Objective-C objects (hence the argument type of addObject: being id).

Are you on the Mac or iOS? If you're on the Mac, You might want to look at NSPointerArray.

You might also be able to be CFMutableArray with custom callback functions, but I haven't tried this myself.


A third option is NSPointerArray.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜