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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:15:17+00:00 2026-06-12T04:15:17+00:00

I have an ivar which is a NSMutableArray . I’ve seen some people use

  • 0

I have an ivar which is a NSMutableArray. I’ve seen some people use this function as a way to initialize an array:

- (NSMutableArray *)varArray
{
   if (!varArray)
      varArray = [[NSMutableArray alloc]init];

   return varArray;
}

And the array is released in dealloc.

When I tried to do this, sometimes the array is initialized, sometimes not.

So what I’m asking is, Is this a good way to initialize an ivar NSMutableArray, or is it better to do this: varArray = [[[NSMutableArray alloc]init]autorelease]; instead?

  • 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-06-12T04:15:18+00:00Added an answer on June 12, 2026 at 4:15 am

    Doing this line:

    varArray = [[NSMutableArray alloc]init];

    Is always fine, but in which function is where it matters. It appears you are initializing it in the variable’s setter, so whenever you query for that instance variable it should always exist… in theory. But issues may occur if you merely dealloc the ivar without setting it to nil so the following will explicitly reset the ivar pointer so that it is guaranteed to be nil when dealloced:

    [varArray dealloc];
    varArray = nil;
    

    This will ensure your if (!varArray) check will be correct.

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

Sidebar

Related Questions

I have an ivar, keys which is an NSMutableArray containing 50 strings. When my
I have an ivar mutable array which i setup in viewDidLoad as follows: names
I have noticed that most Objective-C coders never use the self->ivar syntax when accessing
have written this little class, which generates a UUID every time an object of
I have an NSOperation which fetches some objects from a core data persistent store
I have an ivar which is alloc-inited in the init of an object: attString
I have created a simple class called Engine which has an ivar called inputName
If I have a class hierarchy in which subclasses require use of more specific
I have an ivar which is mentioned in my header @interface MyClass : UIView{
I have some code which waits for a server response & uses a lambda

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.