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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:51:37+00:00 2026-05-31T23:51:37+00:00

I have a singleton as follows, which creates an instance of NSDictionary to hold

  • 0

I have a singleton as follows, which creates an instance of NSDictionary to hold my data. Here is the .h:

@interface FirstLast : NSObject

@property (strong, nonatomic, readonly) NSArray *firstArray;
@property (strong, nonatomic, readonly) NSArray *lastArray;
@property (strong, nonatomic, readonly) NSDictionary *fl;

+ (FirstLast *) firstLast;
- (NSDictionary *) tempDic;

@end

Here is the .m

@implementation FirstLast

@synthesize firstArray = _firstArray;
@synthesize lastArray = _lastArray;
@synthesize fl = _fl;

+ (FirstLast *)firstLast {
    static FirstLast *singleton;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        singleton = [[FirstLast alloc] init];
        NSLog(@"FirstLast instantiated");
    });
    return singleton;
}

- (NSDictionary *) tempDic{

    _firstArray = [[NSArray alloc] initWithObjects:@"Bob", @"Joe", @"Sally", @"Sue", nil];
    _lastArray = [[NSArray alloc] initWithObjects:@"Jones", @"Johnson", @"Thompson", @"Miller", nil];
    _fl = [NSDictionary dictionaryWithObjects:_firstArray
                                    forKeys:_lastArray];
    NSLog(@"tempDic just made _fl at this address");
    NSLog(@"%p", _fl);
    return _fl;
}

@end

All of this works fine. In the view controller I instantiate all this for the first time (works fine too):

 NSLog(@"VC is setting up tempDic");

 [[WordsClues wordsClues] tempDic];

When I try to gain access to tempDic elsewhere, like this:

NSInteger rIndex = arc4random_uniform(4) + 1;
NSString *fname = [[[FirstLast firstLast].tempDic allValues] objectAtIndex:rIndex];

it works fine, but, when I repeat this process, each time I’m creating a new tempDic. I know this because the NSLog giving the address gives a different answer each time. I really want to access the existing dictionary, which is what I thought my singleton was going to accomplish. Clearly I’m either not accessing tempDic correctly or I misunderstand what the singleton can do for me or I have the tempDic set up wrong. The goal is to get a random value from a single copy of tempDic and not write local copies of tempDic all over the place. 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-31T23:51:38+00:00Added an answer on May 31, 2026 at 11:51 pm

    Why do you recreate the dictionary in -tempDic at all?

    I.e. move the dictionary instantiation code to init and then just return _fl; in tempDic.

    No worries — we’ve all been there [new].

    In your FirstLast class, implement the init method as something like:

    - init
    {
         self = [super init];
         if ( self ) {
              _fl = ... create your dictionary here ...;
         }
         return self;
    }
    

    Then change -tempDic to:

    - (NSDictionary*)tempDic {
         return _fl;
    }
    

    I would highly recommend that you read a good intro to Objective-C book. I’m a purist and, thus, would recommend going to the source for the information, but there are lots of books available.

    The questions you are asking are more in line with “What is object oriented programming and how does Objective-C work?”.

    To answer your question; FirstLast is a class and the singleton pattern makes sure there is exactly one instance of that class. By moving the creation of the dictionary to the init method — which is called only once and who stores a reference to the created dictionary in an instance variable — you avoid creating multiple dictionary instances.

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

Sidebar

Related Questions

i Have singleton @interface CartesManager : NSObject { NSMutableArray *carteMan ; NSInteger indexCarteCourante ;
I have a class which, by design, must follow the singleton pattern. So I
I have this Singleton: public enum Elvis { INSTANCE; private int age; public int
I have a Singleton that is accessed in my class via a static property
I have a Singleton which has an NSMutableArray containing a Class, the Class contains
I have a class extending mysqli. It follows a singleton pattern so I have
I have a singleton bean which needs for each call of a function to
I have a singleton class that is instantiated as follows: #import FavoritesManager.h static FavoritesManager
I have a Singleton Class SharedDataObject which has a another class object myClass. MyClass
I have the following situation, i can't resolve: @interface Deck : NSObject @interface MasterDeck

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.