巧用 NSValue 保存对象指针。

 

想必大家都会有这个需求,如何管理物体对象? 用 STD::Vector 吗?

在object c 中我们可以如下操作。 

//Initialize the Array
spriteArray = [[NSMutableArray alloc] init];
 

 

//保存对象.

//Add mySprite to the array
[spriteArray addObject:[NSValue valueWithPointer:mySprite]];

//提取.

mySprite *tmpSprite = nil;
NSValue *pValue = [spriteArray objectAtIndex:0];
[pValue getValue:&tmpSprite];

这样就可以很轻松的管理对象了。

原文地址:https://www.cnblogs.com/cnsoft/p/1405519.html