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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:37:19+00:00 2026-05-24T03:37:19+00:00

Possible Duplicate: Use autorelease when setting a retain property using dot syntax? What is

  • 0

Possible Duplicate:
Use autorelease when setting a retain property using dot syntax?

What is difference between using ivars and self. notation?

instanceVar is instance variable declared with retain.

1) instanceVar = [[NSMutableArray alloc] initWithObjects:@”1″, @”2″]; //do I need autorelease here?????

2) self.instanceVar = [[NSMutableArray alloc] initWithObjects:@”1″, @”2″] autorelease];

Also, Do I need autorelease in the first situation?

  • 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-24T03:37:20+00:00Added an answer on May 24, 2026 at 3:37 am

    This is explained in multiple places but seems as you asked what the different is

    The first call is unchanged and looks like this:

    instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"];
    

    The second call when compiled will look something like this (assuming you have used a @property with retain and @synthesize:

    self.instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"];
    // The previous line will compile to this next line
    [self setInstanceVar:[[NSMutableArray alloc] initWithObjects:@"1", @"2"]];
    

    The body of the - (void)setInstanceVar:(NSMutableArray *)instanceVar; method will look something like this (the compiler create this for you because of your @property and @sythesize):

    - (void)setInstanceVar:(NSMutableArray *)anInstanceVar
    {
        if (instanceVar != anInstanceVar) {
            [instanceVar release];
            instanceVar = [anInstanceVar retain];
        }
    }
    

    Therefore in the call

    self.instanceVar = [[NSMutableArray alloc] initWithObjects:@"1", @"2"];
    

    You have the +1 retain count on the newly created NSMutableArray and then you have the +1 retain count added from going through the setter.

    This means that you require the extra release to match retains you are taking. It is considered better to not use autorelease in iPhone so you can be sure memory is being freed when you want it to. Therefore you should normally take the pattern

    1. Create local var
    2. Assign local var to ivar through setter
    3. release local var

    Which looks like this (FIXED thanks to @jamapag)

    NSArray *tmpMyArray - [[NSArray alloc] initWithObject:@"Hello"];
    self.myArray = tmpMyArray;
    [tmpMyArray release]; tmpMyArray = nil;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Use Visual Studio web.config transform for debugging I have an asp.net application
Possible Duplicate: Use same div to toggle different parts of the page Hello I
Possible Duplicate: Use a variable within a variable? Java Hi this is a general
Possible Duplicate: Use JavaScript to place cursor at end of text in text input
Possible Duplicate: Testing use of NSURLConnection with HTTP response error statuses This is bizarre;
Possible Duplicate: Why use getters and setters? In C#(ASP.NET) we use getter and setter
Possible Duplicate: How to use ternary operator in razor (specifically on HTML attributes)? I
Possible Duplicate: Can I use a binary literal in C or C++? I am
Possible Duplicate: When to use PNG or JPG in iPhone development? I want to
I know its probably possible, but is it practical and doable to try and

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.