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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:51:49+00:00 2026-05-16T14:51:49+00:00

I have numerous classes that use the various NSDictionary/NSArray collection classes as ivars but

  • 0

I have numerous classes that use the various NSDictionary/NSArray collection classes as ivars but often I run into the problem of my collection class getting released before the containing class is released.

This seems to happen mostly with the collections classes and not with another model class (ie classes that I either created separately or other NS* non-collection classes).

Here are the two variations I’ve done and seen other people do:
@implementation ClassX

// myDictionary declared as a property in the .h file as this:
// @property (nonatomic, retain) NSMutableDictionary *myDictionary;

@synthesize myDictionary;

- (id)int
{
    if (self = [super init])
    {
        // Option 1:
        // If I don't instantiate and assign with 'self',
        // myDictionary ivar will not be available 
        // at times in doSomething.

        myDictionary = [NSMutableDictionary dictionary];

        // Option 2:
        // Doing this, however will keep the dictionary around.
        // because I have invoked an extra retain on the dictionary
        self.myDictionary = [NSMutableDictionary dictionary];

        // Which one is more correct?   
    }
    return self;
}

- (void)doSomething
{
    // this will give the error about trying to invoke
    // a method on an already released instance
    [myDictionary objectForKey:@"myKey"];
}

- (void)dealloc
{
    // If I did self.myDictionary in 'init', I then
    // need to do this:
    [myDictionary release];
    [super dealloc];
}
@end

So which approach is the more correct way to hold an instance of NSDictionary within a class?

  • 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-16T14:51:49+00:00Added an answer on May 16, 2026 at 2:51 pm

    Option 2 is correct; Option 1 is wrong.

    But you left out the best option: myDictionary = [[NSMutableDictionary alloc] init].

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

Sidebar

Related Questions

In my program I have numerous Entity classes with a Texture2D attribute that are
We have numerous python classes that do not seem to need __init__ , initialising
I have simple base class with single static field. I have numerous classes that
I have numerous functions (unknown at design time) that each take a specific number
i have strange problem doing reporting: i have numerous clients with different issued invoices.
I have found numerous sources for monitoring battery levels, but none of them describe
I have seen numerous ways to highlight the current tab, but they all have
Scenario is such: I have a webapp that I'd like to run dynamically with
I have two classes called Person and Animal that have a lot of methods
I have a class file that contains all the classes that are needed for

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.