Objective-C 中的对象池
在 Objective-C 中是否有一个很好的方法来做到这一点,或者我必须编写自己的乏味逻辑?
我在 iPhone 游戏中每帧创建和销毁一些小状态对象。如果我可以重用池中的对象,那就太好了。
Is there a nice way to do this in Objective-C, or do I have to write my own tedious logic?
I'm creating and destroying a little of little state objects per frame in an iPhone game. It would be nice if I could just reuse objects from a pool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iPhone 的 Sparrow 框架 包含一个名为“SPPoolObject”的类。该框架在内部将其用于极其常用的辅助对象,例如点、矩形或矩阵。
如果您继承自 SPPoolObject,则“dealloc”方法不会真正删除它;相反,内存将被重新用于下一个分配的对象。
这是一个非常简单的类 - 您可以轻松地将它用于您的项目。它没有任何依赖性,因此您可以轻松地从 Sparrow 框架中获取它;)
The Sparrow framework for the iPhone contains a class called "SPPoolObject". The framework uses it internally for helper objects that are used extremely often, like points, rectangles or matrices.
If you inherit from SPPoolObject, the 'dealloc' method does not really delete it; instead, the memory will be reused for the next alloc'ed object.
It's quite a simple class - you can easily use it for your projects. It does not have any dependency, so you can easily grab it out of the Sparrow framework ;)
Cocoa 和 Objective-C 都没有对对象池做任何特别有帮助的事情。他们也不会做任何事情来阻止你,但你基本上必须自己动手。
Neither Cocoa nor Objective-C does anything particularly helpful for object pools. They don't do anything to stop you either, but you'll basically have to DIY.
我认为表视图对不同的 TableCell 有某种池化机制,
类似这样:
不确定是否有更通用的东西可以使用。
I think that the table View has some sort of pooling mechanism for the different TableCell
something like that :
Not sure if there is something more generic to use.