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 6806769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:45:04+00:00 2026-05-26T19:45:04+00:00

Being fairly new to iPhone / Objective-C development, I wanted to ask this question

  • 0

Being fairly new to iPhone / Objective-C development, I wanted to ask this question to make sure that I’m going about initializing instance variables correctly in different scenarios. So below, I’m going to present a few scenarios and if anyone sees anything being done incorrectly, can they please let me know. (Note: For my examples I’ll be using “instanceVariable” for the instance variable we’re looking to initialize, which is an object of class “InstanceVariableClass”.)

Scenario 1: Initializing in a Non UIViewController Class

a) New Allocation

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        instanceVariable = [[InstanceVariableClass alloc] init];
    }
    return self;
}

In the initializer, it’s OK to access the variable directly (ie not through it’s property) and allocate it. When you call alloc, the newly created object will be automatically retained, which will work perfectly later on when you use it with your getter and setter methods. You don’t want to allocate the variable using a property, i.e. self.instanceVariable = [[InstanceVariableClass alloc] init]; or else you’ll be retaining it twice (once in your setter method, and one with the alloc).

b) Parameter

- (id)initWithFrame:(CGRect)frame object(InstanceVariableClass*) theInstanceVariable {

    self = [super initWithFrame:frame];
    if (self) {
        instanceVariable = [theInstanceVariable retain];
    }
    return self;
}

Once again, OK to directly access your instance variable in the initializer. Since you’re not allocating the variable and simply are wanting to own a copy that was passed into you, you need to have it explicitly retain itself. If you had used your setter method, it would’ve retained it for you, but want to avoid accessing properties in the initializer.

c) Convenience Method

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        instanceVariable = [[InstanceVariableClass returnInitializedObject] retain];
    }
    return self;
}

When using a convenience method to return a new object, you also need to explicitly retain for the same reasons as a parameter. The convenience method (if implemented properly) will have autoreleased the new object it generates, so we don’t have to worry about doubly retaining it.

Scenario 2: Initializing in a UIViewController Class

a) New Allocation

- (void) viewDidLoad // or - (void) loadView if you implemented your view programmatically
{
    [super viewDidLoad];

    InstanceVariableClass *tempInstanceVariable = [[InstanceVariableClass alloc] init];
    [self setInstanceVariable: tempInstanceVariable];
    [tempInstanceVariable release];
}

In a UIViewController, you want to do your instance variable initializing in the viewDidLoad method to employ the practice of lazy loading, or loading in your variables only at the exact moment you need them. Outside of the initializer, it’s bad practice to access the variable directly, so we’ll now use our synthesized setter method to set the variable. You don’t want to allocate the variable using the setter method, ie [self setInstanceVariable] = [[InstanceVariableClass alloc] init]; or else you’ll be retaining it twice (once in your setter method, and one with the alloc). So the best practice is to create a new temp variable, initialize the temp variable, set your instance variable to the temp variable, then release the temp variable. The synthesize setter method will have retained the variable for you.

b) Convenience Method

- (void) viewDidLoad // or - (void) loadView if you implemented your view programmatically
{
    [super viewDidLoad];

    [self setInstanceVariable: [InstanceVariableClass instanceVariableClassWithInt:1]];
}

Initializing an instance variable outside of an initializer method, we can simply use our setter method to set and retain the object that is generated. The convenience method (if implemented properly) will have autoreleased the object it returns, so we don’t have to worry about doubly retaining it.

That’s what I have so far. If anyone can find any flaws in my reasoning, or think of any other scenarios I forgot to include, please let me know. Thanks.

  • 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-26T19:45:05+00:00Added an answer on May 26, 2026 at 7:45 pm

    All the examples you’ve provided are perfectly valid.

    However many experienced obj-c programmers prefer to never access instance variables directly except for inside their set/get methods (which may not even exist if you’re declaring them with @property and @synthesize) unless it’s necessary to avoid some performance bottleneck.

    So, my constructors normally look something like this:

    - (id)initWithFrame:(CGRect)frame {
      self = [super initWithFrame:frame];
      if (self) {
        self.instanceArray = [NSArray array];
      }
      return self;
    }
    

    But I will sometimes choose to write my code exactly as you have done, if profiling the code shows the set/get methods and autoreleass pools are taking up too much CPU time or RAM.

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

Sidebar

Related Questions

I want to preface this question by stating that I am fairly new to
Being fairly new to Spring I have a question about annotating a class. When
I'm fairly new to Ruby and backend development in general. That being said I'm
First let me state that, despite being a fairly new practitioner of TDD, I'm
Being fairly new to C++ I have a question bascially concerning the g++ compiler
I am fairly new to jQuery and I have this overlay window that pops
I'm fairly new to C# but I will try to make this quick! ;)
I'm fairly new to this, and don't have anyone else to ask. I'm attempting
I'm fairly new to core data and iphone programming. Maybe this is an obvious
I'm fairly new to iPhone development and I've hit a roadblock in my understanding

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.