I've been studying the sample code Apple provides in their iPhoneCoreDataRecipes application and there are a couple of things they they're doing that I don't understand and I haven't been able to find a resource to help me with them.
Specifically, the block of code in question is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger section = indexPath.section;
UIViewController *nextViewController = nil; //Why this as opposed to alloc and init?
/*
What to do on selection depends on what section the row is in.
For Type, Instructions, and Ingredients, create and push a new view controller of the type appropriate for the next screen.
*/
switch (section) {
case TYPE_SECTION:
nextViewController = [[TypeSelectionViewController alloc] initWithStyle:UITableViewStyleGrouped];
((TypeSelectionViewController *)nextViewController).recipe = recipe;
break; //Why this as opposed to nextViewController.recipe = recipe???
case INSTRUCTIONS_SECTION:
nextViewController = [[InstructionsViewController alloc] initWithNibName:@"InstructionsView" bundle:nil];
((InstructionsViewController *)nextViewController).recipe = recipe;
break;
case INGREDIENTS_SECTION:
nextViewController = [[IngredientDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
((IngredientDetailViewController *)nextViewController).recipe = recipe;
if (indexPath.row < [recipe.ingredients count]) {
Ingredient *ingredient = [ingredients objectAtIndex:indexPath.row];
((IngredientDetailViewController *)nextViewController).ingredient = ingredient;
}
break;
default:
break;
}
// If we got a new view controller, push it .
if (nextViewController) {
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];
}
}
I'm wondering if the ((name *)name).thing = aThing is different than name.thing = aThing? I'm sure it is, but I can't find any documentation to help me understand what they are doing here and why?
Also, why do they set the nextViewController equal to nil as opposed to just allocating and initializing it? Is there a reason that this was done here, while in other areas when creating a temporary view controller they used alloc and init?
Thanks in advance for your help!