I have an NSArray that looks like this:
ArrayItem *thisone = [[ArrayItem alloc] initWithName:@"The Name" data1:@"Some data" data2:@"Some more data" image:@"theimage.jpg" range:0];
ArrayItem *thisone1 = [[ArrayItem alloc] initWithName:@"The Name" data1:@"Some data" data2:@"Some more data" image:@"theimage.jpg" range:0];
ArrayItem *thisoneagain = [[ArrayItem alloc] initWithName:@"The Name 2" data1:@"Some data2" data2:@"Some more data2" image:@"theimage2.jpg" range:1];
ArrayItem *thisoneagain1 = [[ArrayItem alloc] initWithName:@"The Name 2" data1:@"Some data2" data2:@"Some more data2" image:@"theimage2.jpg" range:1];
ArrayItem *thisoneagain2 = [[ArrayItem alloc] initWithName:@"The Name 2" data1:@"Some data2" data2:@"Some more data2" image:@"theimage2.jpg" range:1];
ArrayItem *thisoneyetagain = [[ArrayItem alloc] initWithName:@"The Name 3" data1:@"Some data3" data2:@"Some more data3" image:@"theimage3.jpg" range:2];
ArrayItem *thisoneyetagain1 = [[ArrayItem alloc] initWithName:@"The Name 3" data1:@"Some data3" data2:@"Some more data3" image:@"theimage3.jpg" range:2];
ArrayItem *thisoneyetagain2 = [[ArrayItem alloc] initWithName:@"The Name 3" data1:@"Some data3" data2:@"Some more data3" image:@"theimage3.jpg" range:2];
ArrayItem *thisoneyetagain3 = [[ArrayItem alloc] initWithName:@"The Name 3" data1:@"Some data3" data2:@"Some more data3" image:@"theimage3.jpg" range:2];
self.ArrayItems = [[NSMutableArray alloc] initWithObjects:thisone,thisone1,thisoneagain,thisoneagain1,thisoneagain2,thisoneyetagain,thisoneyetagain1,thisoneyetagain2,thisoneyetagain3,nil];
and I would like to count the number of items in each “range” (as in there are 2 items with a range of 0, 3 with a range of 1, and 4 with a range of 2) and store the number of items for each range in separate variables. Any idea how to do this? ArrayItems.count just tells me how many there are in total.
This is more of a general programming question rather than iOS specific, but I will give you the answer using Objective-C.
After you create your array you can do the following:
This will give you the numer of items on each range stored on the range0, range1 and range2 variables.
Using Tim’s Approach you can do something like this: