2012-04-13 85 views
0

我想了解如何使用2D中的每條語句NSMutableArray。我的代碼如下。它在第三條語句(最內部)處引發異常。唯一的例外是:多維數組迭代在objective-c

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance"

我的代碼:

NSMutableArray* subTryingSet=[NSMutableArray arrayWithArray:[self genSetNumbers:arrRandoms withSize:4]]; 

for (NSMutableArray* oneRow in subTryingSet) { 
    for (NSMutableArray* w in oneRow) { 
     for (int i=0;i<w.count;i++) { 
      NSLog(@"%d", [[w objectAtIndex:i] intValue]); 
     } 
    } 
} 

回答

3

第一快看看你的代碼之後:

嘗試改變這一點:

NSLog(@"%d", [[w objectAtIndex:i] intValue]); 

有:

NSLog(@"%i", [[w objectAtIndex:i] intValue]); 

編輯

「它在拋出異常3TH‘的說法’,所以不能去苦幹」

嗯...你確定在oneRow所有對象都是NSMutableArray中?

嘗試檢查這樣的:

+0

它拋出異常,在3TH「的說法」,所以不能去NSLog的。 – lykant 2012-04-13 09:56:11

+0

在我的回答中看到新的編輯 – meronix 2012-04-13 10:01:28

+0

好吧,你對meronix。這裏,w不是一個數組。我會看看爲什麼genSetNumbers的方法返回「subTryingSet」作爲一維數組。非常感謝你。我可能會再問一些關於這種情況的問題。 :) – lykant 2012-04-13 10:20:45

0

您可以使用此自定義目標C方法循環

-(void)loopMultArray:(NSArray*)a walk:(void(^)(id node,int index,int zindex))n{ 

    void(^callback)(id node,int index,int zindex) = Block_copy(n); 

    NSMutableArray *l=[[[NSMutableArray alloc] initWithObjects:a,nil] autorelease]; 
    int c=1; 
    //This first loop will loop until the count var is stable// 
    for(int r=0;r<c;r++){ 
     //This loop will loop thru the child element list// 
     for(int z=0;z<[[l objectAtIndex:r] count];z++){ 

      callback([[l objectAtIndex:r] objectAtIndex:z],z,r); 

      if([[[l objectAtIndex:r] objectAtIndex:z] isKindOfClass:[NSArray class]]){ 
       [l addObject:[[l objectAtIndex:r] objectAtIndex:z]]; 
       c++; 
      }//IF 
     }//FOR 
    }//FOR 

    Block_release(callback); 

}