2012-01-25 27 views
1

我試圖設置一個簡單的基於Mac的2D平鋪引擎,使用2D NSMutableArray進行映射。爲了保持MVC的完整性,我使用NSViewController的子類,引用地圖對象(包含所述數組),並將瓦片數據的繪圖請求通過它傳遞給地圖。但是,我的應用程序似乎不會在我的drawRect代碼開始觸發前用對象填充數組。NSMutableArray在需要加載之前未被填充

每當我運行這個應用程序時,我的窗口無法加載,並且在調試器中收到錯誤消息「 - [__ NSArrayM objectAtIndex::index 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循環之前被調用 - 而obvio好吧,我的drawRect代碼必須引用數組,然後從那裏快速失敗。我一定是做錯了什麼,或者做錯了什麼,但我無法弄清楚它可能是什麼。

回答

2

您有==而不是!=,因此您的for循環都不會執行。我認爲循環應該如下:

for (int i = 0; i != 15; i++) 
{ 
    NSMutableArray *tempRow = [NSMutableArray arrayWithCapacity:20]; 
    for (int j = 0; j != 20; j++) 
    { 
     NSNumber *tempID = [NSNumber numberWithInt:0]; 
     [tempRow addObject:tempID]; 
    } 
    [map addObject:tempRow]; 
} 
+0

事實上,就是這樣。非常感謝你指出今天早上我的大腦拒絕了什麼。 :) –