0

I want to learn how I can use for-each statements in 2D NSMutableArray. My code is below. It throws an exception at the 3rd (inner-most) for statement. The exception is:

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

My code:

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]);
        }
    }
}

2 Answers 2

3

after a first fast look at your code:

try to change this:

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

with:

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

EDIT

"It throws exception at 3th "for statement", so it can't go to slog"

mmm... are you sure that all the objects in oneRow are NSMutableArray?

try to check like this:

for (NSMutableArray* oneRow in subTryingSet) {
  if ([oneRow.class isSubclassOfClass:[NSMutableArray class]]) {
      for (NSMutableArray* w in oneRow) {
        if ([w.class isSubclassOfClass:[NSMutableArray class]]) {
            for (int i=0;i<w.count;i++) {
                NSLog(@"%d", [[w objectAtIndex:i] intValue]);
            }
        }
      }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It throws exception at 3th "for statement", so it can't go to nslog.
ok, you r right meronix. here, w is not an array. i will look why method of genSetNumbers returns "subTryingSet" as one dimensional array. thank you very much. i may ask some questions again about this situation. :)
actually, the second for must be as "for(NSNumber *w in oneRow)". This was a confusion for me.
0

You can use this custom objective c method to iterate

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.