Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
TJ Rogers
8,758 PointsUsing an "If" conditional within a "For In" statement
My understanding of the "For-In" statement - where the "For" refers to an NSDictionary object and the "In" refers to an NSArray of dictionary objects - is that the statement will iterate through each dictionary object in the array and perform whatever command is subsequently included in the For-In statement, and do this for each instance of the dictionary object in the Array.
So I'm a little perplexed why an "If" conditional that I include in the "For-In" executes only once, and then stops all subsequent iterations. Here is my code:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ( [segue.identifier isEqualToString:@"showMuniType"] ) {
NSMutableArray *passToTown = [[NSMutableArray alloc] init];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *oyYearRow = [self.oyYear objectAtIndex:indexPath.row];
for (NSDictionary *yearSegue in self.yearData) {
HeaderData *year2 = [[HeaderData alloc] init];
HeaderData *town2 = [[HeaderData alloc] init];
year2.year = [yearSegue valueForKey:@"year"];
town2.Town = [yearSegue valueForKey:@"town"];
if ( oyYearRow == year2.year ) {
[passToTown addObject:town2.Town];
}
}
NSLog(@"%@", passToTown);
}
}
What I'm trying to do is this:
There are 1,700 dictionary objects in my array, each with a key for "Year" and "Town". I'm trying to create a segue, where an array of towns is passed to another tableview based on the year selected. I figured the best way to do this would be a "For-If" statement to iterate through the array, and then an "If" statement to select only those towns coupled with the selected year.
But when I test the array that will be passed through the segue with the NSLog, I get only one Town object in my array. I'm certain it's the "If" statement that's causing the "For-If" to stop iterating because when I comment out the "If" statement I get all 1,700 Town objects in my the array.
Thoughts?
2 Answers
John W
21,558 PointsThis is a common mistake. You are trying to test equivalence between two NSStrings, which are objects, so == won't work unless you want to compare exactly the addresses of the objects in memory, which is why you only added 1 object. Use the isEqualTo: method instead.
TJ Rogers
8,758 PointsThanks John W!
I poured over this problem for several hours before posting to the forum - putting NSLogs and breaks everywhere, searching stackoverflow, Googling stuff - everything I could think of to get to the bottom of the issue. Eventually I figured it was probably something stupid and small that lay outside the scope of my knowledge. Guess I was right. At least I'll never make that mistake again.
Thanks for the intervention. I'm a big fan of Team Treehouse. Learning a ton from you guys!
John W
21,558 Points:)