2
  NSMutableArray*array1=[NSMutableArray arrayWithObjects: @"1", @"2", @"3", nil];
  NSMutableArray*array2=[NSMutableArray arrayWithObjects: @"A", @"B", @"C",@"D", nil];
  NSMutableArray*array2=[NSMutableArray arrayWithObjects: @"AA", @"BB", @"CC", nil];

  NSMutableArray*MasterArray=[NSMutableArray arrayWithObjects: array1,array2,array3, nil];

How to get the object count of each array present in MasterArray?

1
  • 1
    You can create arrays more cleanly with modern Objective-C syntax: NSArray* array = @[@"1", @"2", @"3"]; This is called an array literal Commented Feb 8, 2014 at 3:26

3 Answers 3

3

If it isn't what you want, tell me.

[MasterArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[NSArray class]]) {
        NSLog(@"%lu", [obj count]) ;
    }
}] ;
Sign up to request clarification or add additional context in comments.

1 Comment

I prefer this answer. But fast enumeration is much more faster then block-base API.
1

You can use fast enumeration...

for(NSArray* array in MasterArray) {
    NSLog(@"array:%@ count:%d", array, [array count]);
}

Comments

0

There are a few things I'd like to point out here. First of all, the code you've posted can be simplified dramatically using array literals.

NSMutableArray *array1 = [@[@"1",@"2",@"3"] mutableCopy];
NSMutableArray *array2 = [@[@"A",@"B",@"C",@"D"] mutableCopy];
NSMutableArray *array3 = [@[@"AA",@"BB",@"CC"] mutableCopy];

NSMutableArray *masterArray = [@[array1,array2,array3] mutableCopy];

Then theres the matter of your naming of MasterArray. In Objective-C, it's common to use lowerCamelCase formatting for instances, and UpperCamelCase for classes. Conforming to this style will help improve your code's readability.

Now, on to your actually problem. If you can get the count of an array just by accessing its count property, then there's no reason why you need a named variable specifically to access the array. Consider the following two implementations to access the count of the first sub array within an array. They both do the same thing:

NSArray *arr = @[subArray1,subArray2,subArray3];
NSArray *sub1 = arr[0];
NSUInteger countOfSub1 = sub1.count;

NSArray *arr = @[subArray1,subArray2,subArray3];
NSUInteger count = [arr[0] count];

So then if you want to get the count of every sub array in the master array, you can achieve the same thing inside a loop.

for(NSMutableArray *subArray in masterArray) {
    NSUInteger count = subArray.count;
}

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.