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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:39:23+00:00 2026-06-13T08:39:23+00:00

I’ve encountered a stupid problem, and I’ve tried almost everything (bought 3 books, went

  • 0

I’ve encountered a stupid problem, and I’ve tried almost everything (bought 3 books, went through the whole google :)) but nothing helped. And it seems to me like the solution should be extremely simple…

I need to declare a singleton in Objective-C (for an iOS app, if that matters), and it should have some properties that I need to update from other classes. But I can’t do that – the properties just won’t update, they have the same values set in the “init” method.

I’ve created a simple app to test out this problem. That’s what I’ve done:

First, I’ve declared a sample class and its subclass that I’m going to use as a singleton’s property:

@interface Entity : NSObject

@property (nonatomic, strong, readwrite) NSMutableString * name;

@end

@implementation Entity
@synthesize name;

@end

@interface Company : Entity

@property (nonatomic, strong, readwrite) NSMutableString * boss;
@property (nonatomic) int rating;

@end

@implementation Company
@synthesize boss, rating;

@end

Then I declare the singleton itself based on the method described in the “iOS Programming Guide by Big Nerd Ranch” book. I’m using both my custom class and a standard NSMutableString as properties, just for clarity’s sake:

@class Company;

@interface CompanyStore : NSObject
{
     NSMutableString * someName;
}

@property (nonatomic, strong, readwrite) Company * someCompany;
@property (nonatomic, strong, readwrite) NSMutableString * someName;

+ (CompanyStore *) store;
- (void) modifyCompanyProperties;

@end

@implementation CompanyStore
@synthesize someCompany, someName;

// Declaring the shared instance
+ (CompanyStore *) store
{
    static CompanyStore * storeVar = nil;

    if (!storeVar) storeVar = [[super allocWithZone:nil] init];

    return storeVar;

}

// Replacing the standard allocWithZone method
+ (id) allocWithZone:(NSZone *)zone
{
    return [self store];
}

Then I initialize all the properties with initial values:

- (id) init
{
    self = [super init];
    if (self) {
        someCompany = [[Company alloc] init];
        [someCompany setBoss:[NSMutableString stringWithFormat:@"John Smith"]];
        [someCompany setName:[NSMutableString stringWithFormat:@"Megasoft"]];
        [someCompany setRating:50];

        someName = [[NSMutableString alloc] initWithString:@"Bobby"];
    }
    return self;
}

And from another class (view controller that displays the contents in a view):

1. I get the values of the singleton’s properties. Everything’s okay – I get “John Smith”, “Megasoft”, “Bobby” and 50 for my int value. The values from my init method.

2. I change the singleton’s properties from that view controller (using several ways – I’m not sure now which one is right):

- (IBAction)modify2Button:(id)sender {

    CompanyStore * cst = [CompanyStore store];

    NSMutableString * name = [[NSMutableString alloc] initWithString:@"Microcompany"];
    NSMutableString * boss = [[NSMutableString alloc] initWithString:@"Larry"];

    [[[CompanyStore store] someCompany] setName:name];
    cst.someCompany.boss = boss;

    NSMutableString * strng = [[NSMutableString alloc] initWithString:@"Johnny"];
    [cst setSomeName:strng];
}

… and then I’m trying to get the values again. I’m still getting the old set – “John Smith”, “Megasoft” etc. even though when I set a breakpoint at one of the strings, I can see that singleton’s name property is “Microcompany” and not “Megasoft” at the time of the break… But it doesn’t seem to be assigned.

3. Then I’m trying another thing – I’m calling from the view controller a singleton’s private method, which assigns another set of values to the properties. This is that method in the singleton:

- (void) modifyCompanyProperties
{
    NSMutableString * boss = [[NSMutableString alloc] initWithString:@"George"];
    NSMutableString * name = [[NSMutableString alloc] initWithString:@"Georgeland"];

    [someCompany setBoss:boss];
    [someCompany setName:name];
    [someCompany setRating:100000];

    [someName setString:@"Nicholas"];

}

4. I’m trying to get the updated property values from the view controller again… and still get those “John Smith”, “Megasoft”… Nothing changes.

It seems like the properties of the singleton are set only once and then I can’t change them, even though their attributes are declared as “readwrite”.

It looks like I don’t understand something simple.
If someone could explain how to correctly declare and update properties in singletons, I would be very grateful.

  • 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-13T08:39:24+00:00Added an answer on June 13, 2026 at 8:39 am

    First thing I noticed was that you are declaring “storeVar” in the body of the store method. And this looks like terribly wrong to me because every time you call this you’ll re-initialize the singleton. You should declare the variable like this:

    static CompanyStore * storeVar = nil;
        @implementation CompanyStore
        @synthesize someCompany, someName;
    
        // Declaring the shared instance
        + (CompanyStore *) store
        {
    
            if (!storeVar) storeVar = [[super allocWithZone:nil] init];
    
            return storeVar;
    
        }
    

    Also your init method is not exactly complete because you don’t want to call init again after the singleton has been initialized so you have to check this and if it has been initialized you should simply return it:

    - (id) init
    {
    if (storeVar!=nil) {
    return storeVar;
    }
    
        self = [super init];
        if (self) {
            someCompany = [[Company alloc] init];
            [someCompany setBoss:[NSMutableString stringWithFormat:@"John Smith"]];
            [someCompany setName:[NSMutableString stringWithFormat:@"Megasoft"]];
            [someCompany setRating:50];
    
            someName = [[NSMutableString alloc] initWithString:@"Bobby"];
        }
        return self;
    }
    

    Also, this is not a mistake, just a mere suggestion – you can ditch @synthesize because since ios 6 because the compiler generates it automatically. But again, not a mistake to use it. Hope it helps

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from

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.