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!
I’m going to answer your questions in the order they come up in the code rather than the order you asked them, because the order is important here.
The reason we don’t create the view controller when we first declare the variable is because we don’t know what we want to create yet. Think about it: You want to write
alloc] init]. But what goes before thealloc? We don’t really know yet. We know it’s some subclass of UIViewController, but we don’t know the actual class. That’s what the next part of the code is for.So now we get to the main logic of the method. We’re assigning the view controller’s recipe, but there’s a problem here: We declared the variable as
UIViewController*, and UIViewController doesn’t have arecipeproperty. We know our subclass does, but we already told the compiler that this variable pointed to a UIViewController. So in order for the compiler to let us do this property assignment, we need to cast the variable to the right class.Incidentally, we could also just have declared the variable as
id nextViewControllerand then write[nextViewController setRecipe:recipe]and there wouldn’t be any need to cast. The downside is that then the compiler wouldn’t be able to typecheck our code — we could accidentally assign an NSString tonextViewControllerin some little-used branch of the code and we’d never know until we happened to hit that branch at runtime.