如何在iOS项目中实现CHCircularBuffer?
对于我的游戏 iOS 项目,我需要一个环形缓冲区。它的工作方式应该类似于队列,其中元素出入,但缓冲区中的元素总数应保持不变。
我使用java成功实现了环形缓冲区,但我对objective-c不太熟悉。我在网上找到了一个名为 CHCircularBuffer 的环形缓冲区实现: https://bitbucket .org/devartum/chdatastructs/src/4d6d7194ee94/source/CHCircularBuffer.m 但是我未能实现正确地。
循环缓冲区是名为 TerrainManager 的类的属性,该类执行所有数学地形生成。
@interface TerrainManager : NSObject{
int terrainParts;
CHCircularBuffer* circularTerrainBuffer;
}
@property(nonatomic, retain) CHCircularBuffer *circularTerrainBuffer;
@end
这就是在 TerrainManager 的实现中初始化环形缓冲区的方式。
circularTerrainBuffer = [[CHCircularBuffer alloc] initWithCapacity:parts];
这将创建缓冲区的实例并将大小属性设置为部件。现在,我使用 addObject 方法将对象添加到缓冲区:
[circularTerrainBuffer addObject:[NSNumber numberWithDouble:0.2]];
有时此行会收到错误“exec_bad_access”。例如,当我初始化容量为 15 的缓冲区时,一切都很好,当我初始化容量为 20 的缓冲区时,我会收到错误。
我现在尝试从进行绘图的地形类访问缓冲区。但每当我尝试访问对象时,我都会收到“bad_access”错误。
NSArray *arr = [terrainManager.circularTerrainBuffer allObjects];
例如,这一行会产生错误。
所以我的代码有问题。也许我不理解缓冲区并以错误的方式添加对象。我不知道。有什么想法或建议吗?
for my game iOS project I need a ring buffer. It should work similar to a queue where elements go out and go in but the total amount of elements in the buffer should stay the same.
I implemented the ring buffer successfully using java but I am not so familar with objective-c. I found a ring buffer implementation on the web called CHCircularBuffer: https://bitbucket.org/devartum/chdatastructures/src/4d6d7194ee94/source/CHCircularBuffer.m However I failed implementing it correctly.
The circular buffer is a property of a class called TerrainManager which does all the mathematical terrain generation.
@interface TerrainManager : NSObject{
int terrainParts;
CHCircularBuffer* circularTerrainBuffer;
}
@property(nonatomic, retain) CHCircularBuffer *circularTerrainBuffer;
@end
This how the ring buffer is initialized in the implementation of TerrainManager
circularTerrainBuffer = [[CHCircularBuffer alloc] initWithCapacity:parts];
This creates an instance of the buffer and sets the size property to parts. Now I add objects to the buffer using addObject method:
[circularTerrainBuffer addObject:[NSNumber numberWithDouble:0.2]];
Sometimes this line receives an error "exec_bad_access". E.g. when I init the buffer with capacity of 15 everything is fine, with 20 I get the error.
I now try to access the buffer from the terrain class where the drawing takes place. But whenever I try to access the objects I get an "bad_access" error.
NSArray *arr = [terrainManager.circularTerrainBuffer allObjects];
E.g. this line would create the error.
So there is something wrong with my code. Maybe I dont understand the buffer and add objects the wrong way. I dont know. Any ideas or suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您显示的代码片段是正确的。我实现了一个小项目来测试
CHCircularBuffer
按照您指定的方式并且它工作正常。所以,问题一定出在其他地方。恕我直言,解决这个问题的唯一方法是在失败的行上放置一个断点,然后进入 addObject 函数以查看到底在哪里失败。该数组可以在那里重新分配,因此可能会失败并提供错误的访问权限。
allObjects
也是如此。不管怎样,我不得不说我可以毫无问题地执行我的测试,添加对象,从头部和尾部删除它们,并毫无问题地获取所有对象的数组。
如果您发布更多代码,我们也许可以提供更多帮助。
The snippets of code you are showing are correct. I implemented a small project to test
CHCircularBuffer
the way you specify and it works correctly. So, the problem must be somewhere else.The only way around this is, IMHO, put a breakpoint on the line that fails and step into the
addObject
function to see where exactly it fails. The array could be reallocated in there, so may be this is failing and giving the bad access. The same forallObjects
.Anyway, I have to say that I could execute my test without any issue, adding objects, removing them from head and tail, and getting the array of all objects with no issues.
If you post more code, we can maybe help a bit more.