2012-04-13 11 views
0

2Dののfor-eachステートメントの使い方を知りたい。私のコードは以下の通りです。それは3番目(最も内側)のforステートメントで例外をスローします。例外は次のとおりです。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]); 
これを変更する

試してみます

EDIT

文に対して「それは、第3回では例外をスローします 『』ので、それは強打に行くことができない」

がうーん...あなたは確信しているoneRow内のすべてのオブジェクトがNSMutableArrayのあること?

次のようにチェックしてみてください:

+0

を反復するために、このカスタムObjective Cのメソッドを使用することができますnslog。 – lykant

+0

私の答えで新しい編集を参照してください – meronix

+0

ok、you r right meronix。ここで、wは配列ではありません。私は、genSetNumbersのメソッドが1次元配列として "subTryingSet"を返す理由を見ていきます。どうもありがとうございました。私はこの状況についていくつかの質問を再びするかもしれません。 :) – lykant

0

それが行くことができないようにするには、「ステートメントに対して」それは第3回で例外をスロー

-(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); 

}