I have these arrays set up to go into a NSDictionary
and then they are added to an NSMutableArray
. This is to set up sections.
NOTE: The content of the arrays are a bit random as I am just trying something out.
Now the bit I was expecting is that the word "Awesome!!" would only be re-iterated 3 x 3 times as the counts of both the NSMutableArray
and headArray
are only 3.
So why is it re-iterated 4 x 3 times when I use the <=
operator sign? Should this not be done 3 times as it is matching the count to be only 3 times?
Now I appreciate that the section count is set to 0 which could be an explanation, but it needs to be 0 to be at the top of the UITableView.
listOfItems = [[NSMutableArray alloc] init];
headerArray = [[NSArray alloc] initWithObjects:@"Country 1", @"Country 2", @"Country 3", nil];
NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Iceland", @"Greenland", @"Switzerland", @"Norway", @"New Zealand", @"Greece", @"Italy", @"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];
NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"India", @"U.S.A", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];
NSArray *thirdArray = [NSArray arrayWithObjects:@"Row 01", @"Row 02", @"Row 03", @"Row 04", nil];
NSDictionary *thirdDictionary = [NSDictionary dictionaryWithObject:thirdArray forKey:@"Countries"];
if (headerArray.count == listOfItems.count)
{
for (section = 0; section <= listOfItems.count; section++)
{
NSLog(@"AWESOME!!");
}
}
2013-02-17 20:10:06.323 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.324 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.325 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.326 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.327 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.328 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.330 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.331 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.332 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.332 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.334 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.335 TableView[3667:11303] AWESOME!!
No. What you're doing would be appropriate for 1-indexed arrays. NSArray
, however, is 0-indexed. What happens in your for loop is that section
goes from 0
to listOfItems.count
, that is from 0 to 3, which means 4 iterations (one for 0, 1, 2, and 3, respectively). Use the <
operator instead.
See the Wikipedia article on half-open intervals and zero-based numbering.