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

  • SEARCH
  • Home
  • 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 595605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:04:50+00:00 2026-05-13T16:04:50+00:00

I have always wondered why all apple code samples use code like this: UINavigationController

  • 0

I have always wondered why all apple code samples use code like this:

UINavigationController *aNavigationController = [[UINavigationController alloc]
          initWithRootViewController:rootViewController];

self.navigationController = aNavigationController;

[self.view addSubview:[navigationController view]];

[aNavigationController release];

They always create a local variable and assign it to the ivar why don’t they simply do this:

self.navigationController = [[UINavigationController alloc]
          initWithRootViewController:rootViewController];;

[self.view addSubview:[navigationController view]];

[navigationController release];

Is there any other reason, besides it is easier to understand?. Is this a best practice?.

-Oscar

  • 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-13T16:04:50+00:00Added an answer on May 13, 2026 at 4:04 pm

    Your replacement code is incorrect, and thus illustrates the problem that Apple is trying to prevent. Here is your code:

    self.navigationController = [[UINavigationController alloc]
          initWithRootViewController:rootViewController];
    
    [self.view addSubview:[navigationController view]];
    
    [navigationController release];
    

    You left out the “self.” in your references. Perhaps you meant to access the ivar directly, but in that case you have created very confusing code by mixing accessors and direct ivar access (and violated the cardinal rule by using direct ivar access outside an accessor). If not, then you meant to write this:

    self.navigationController = [[UINavigationController alloc]
          initWithRootViewController:rootViewController];
    
    [self.view addSubview:[self.navigationController view]];
    
    [self.navigationController release];
    

    That last line is very wrong. Never send -release to the result of a method call. So no, the way you’re doing it isn’t correct.

    That said, Apple and I disagree on how to do this. Here’s how I do it:

    self.navigationController = [[[UINavigationController alloc]
          initWithRootViewController:rootViewController] autorelease;
    
    [self.view addSubview:[self.navigationController view]];
    

    I like -autorelease because I find it prevents errors. The further apart the alloc and release get, the more likely a developer will inject a memory leak (by adding a “return” for instance). autorelease avoids this by keeping the retain and release together, making the intent to use this as a temporary variable more clear, and generally makes code review much easier.

    Apple tends to disagree with me in their example code because they’re emphasizing performance by using release versus autorelease. I find this to be the wrong optimization, since this object won’t be deallocated during this run loop in either case (so no memory is saved), and I believe the very small performance penalty of autorelease is more than made up for by the reduction in memory leaks due to incorrect use of release.

    The autorelease vs. release debate is filled with shades of gray (I certainly use release directly in loops), and different developers have different approaches, but your replacement code isn’t the right way to do it in either case.

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

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's actually 2083 characters for a URL (which means all… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer Decompose the table into three different tables. One holds the… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer One patch per issue posted to the projects issue tracker.… May 17, 2026 at 2:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have always wondered how delegates can be useful and why shall we use
I have always wondered what's the best way to achieve this task. In most
I have always wondered why so many Java developers use .do as the extension
This is something I've always wondered about. I understand that horizontal scaling is about
I've always wondered why the C++ Standard library has instantiated basic_[io]stream and all its
While filling out secure forms on bank websites, I have always wondered how they
As a hobby project, I had like to implement a Morse code encoder and
In thinking about blocks, I've always wondered why the thingers.each { |thing| example is
I have been a dabbling into delphi off and on for years and I
When using simple DTOs in various scenarios I have frequently run into the same

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.