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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:34:31+00:00 2026-05-20T00:34:31+00:00

Imagine I have a member variable @property(nonatomic,retain)NSArray *array; Now in my viewDidLoad I set

  • 0

Imagine I have a member variable

@property(nonatomic,retain)NSArray *array;

Now in my viewDidLoad I set up a simple array

array = [[NSArray alloc]initWithObjects:@"A",@"B","C",nil];

My retain count for array is going to be 1 right?

Now if I were to set up the array using the accessor method

self.array = [[NSArray alloc]initWithObjects:@"A",@"B","C",nil];

Is my retain count 2 because my accessor method bumps the retain count up by 1?

What’s the convention for initializing member variables?

  • 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-20T00:34:32+00:00Added an answer on May 20, 2026 at 12:34 am

    That is correct, the retain count for self.array = ends up as 2.

    First, you alloc init a new NSArray object. That’s a retain count of 1. Second, your setter sends the object a retain message when assigning it to your instance var. That bumps the retain count up to 2.

    What’s the convention for initializing member variables?

    Besides directly setting the ivar array = as in your question, here are some ways to do this with your self.array property without leaking:

    1. Autorelease:

      self.array = [[[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil] autorelease];
      
    2. Use the arrayWithObjects: class method. Simpler, and also produces an autoreleased object:

      self.array = [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
      
    3. Make a temporary variable to hold the new pointer, then release it after setting the property (which will have had it retained by then):

      NSArray *tmpArray = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil];
      self.array = tmpArray;
      [tmpArray release];
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine you have a class with dozens of private member variables. Each member variable
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
Imagine I have a property defined in global.asax. public List<string> Roles { get {
Let's say I have a simple class Cat in C#, with a Name property
Imagine I have the following string : set(SEXY_STRING I love CMake) then I want
Imagine we have a program trying to write to a particular file, but failing.
Imagine I have an function which goes through one million/billion strings and checks smth
Imagine I have String in C#: I Don’t see ya.. I want to remove
Imagine I have a chunk of initialisation code at the top of a stored
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into

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.