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.
First thing I noticed was that you are declaring “storeVar” in the body of the
storemethod. 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: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:
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