Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 900829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:27:17+00:00 2026-05-15T15:27:17+00:00

I’ve been studying the sample code Apple provides in their iPhoneCoreDataRecipes application and there

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:27:17+00:00Added an answer on May 15, 2026 at 3:27 pm

    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 the alloc? 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 a recipe property. 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 nextViewController and 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 to nextViewController in some little-used branch of the code and we’d never know until we happened to hit that branch at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.