NSMutableArray 在需要加载之前没有被填充
我正在尝试设置一个简单的基于 Mac 的 2D 平铺引擎,使用 2D NSMutableArray 进行映射。我使用 NSViewController 的子类来引用地图对象(其中包含所述数组),并将图块数据的绘制请求通过它传递到地图,以维护 MVC 完整性。但是,在我的drawRect代码开始触发之前,我的应用程序似乎没有用对象填充数组。
每次我按照编写的方式运行此应用程序时,我的窗口都无法加载,并且我在调试器中收到错误消息“-[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围”。据我所知,在实际显示或需要绘制视图之前,我的数组应该完全初始化为零。
这是我的 NSViewController 子类中的 -loadView 方法:
- (void)loadView
{
currentMap = [[TestMap alloc] init];
[super loadView];
}
这是我的地图对象中的 init 方法:
- (id)init
{
dimensions = NSMakeSize(15.0,20.0);
tileset = [[TestTileset alloc] init];
map = [NSMutableArray arrayWithCapacity:15];
for (int i = 0; i == 14; i++)
{
NSMutableArray *tempRow = [NSMutableArray arrayWithCapacity:20];
for (int j = 0; j == 19; i++)
{
NSNumber *tempID = [NSNumber numberWithInt:0];
[tempRow addObject:tempID];
}
[map addObject:tempRow];
}
return self;
}
断点的明智使用表明
[super loadView]
在 init 开始其 for 循环之前以某种方式调用了该方法 - 显然,我的 drawRect 代码必须引用阵列从那里继续并很快失败。我肯定做错了什么或者不按顺序做事,但我不知道它可能是什么。
I'm attempting to set up a simple Mac-based 2D tiling engine, using a 2D NSMutableArray for mapping purposes. I am using a subclass of NSViewController with a reference to the map object (which contains said array) and passing drawing requests for tile data through it to the map in the interests of maintaining MVC integrity. However, my application seems to not be populating the array with objects before my drawRect code begins firing.
Every time I run this app as written, my window fails to load and I receive the error message "-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array" in the debugger. As far as I can tell, my array should be completely initialized with zeroes before the view is actually displayed or needs to be drawn.
Here is the -loadView method from my NSViewController subclass:
- (void)loadView
{
currentMap = [[TestMap alloc] init];
[super loadView];
}
and here is the init method from my map object:
- (id)init
{
dimensions = NSMakeSize(15.0,20.0);
tileset = [[TestTileset alloc] init];
map = [NSMutableArray arrayWithCapacity:15];
for (int i = 0; i == 14; i++)
{
NSMutableArray *tempRow = [NSMutableArray arrayWithCapacity:20];
for (int j = 0; j == 19; i++)
{
NSNumber *tempID = [NSNumber numberWithInt:0];
[tempRow addObject:tempID];
}
[map addObject:tempRow];
}
return self;
}
Judicious use of breakpoints has indicated that
[super loadView]
is somehow being called before init begins its for loops - and obviously, my drawRect code which has to reference the array follows on from there and fails quickly. I must be doing something wrong or out of order, but I cannot figure out what it might be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有
==
而不是!=
,因此您的for
循环都不会执行。我认为循环应该如下:You have
==
instead of!=
, so neither of yourfor
loops executes. I think the loops should be as follows: