Question

I would like to be able to call a method from any class which changes the value of my global variable.

I'll first outline the problem for anyone who doesn't wish to view the code.

Apologies in advance for the long post! I just want all the details to be available.

In my current setup (a bit of test code inspired by an SREE blog), I have a singleton class (GlobalData) which defines a global variable (globalMessage) and a method (globalFunction) that changes the global variable.

So far, my code...

1) Sets globalMessage as "Default Global Message" in the singleton class (GlobalData).

2) Uses NSLog to print "Outside class: 'Default Global Message' " in another class (MyDocuments).

3) Uses the other class (MyDocuments) to call globalFunction.

4) globalFunction sets globalMessage to "New Global Message"

5) globalFunction uses NSLog(@"Set %@", globalMessage) outputting "Set New Global Message"

6) repeats step 2. NSLog(@"Outside class: %@", [GlobalData sharedGlobalData].globalMessage)

The issue is, on step 6, the console still prints "Default Global Message"

Somehow when I call globalFunction, I am only changing the instance-variable globalMessage and not the global-variable.

Thanks very much for the help!

Here's the code...

GlobalData.h

#import <Cocoa/Cocoa.h>

@interface GlobalData : NSObject {
    NSString *globalMessage;
}

@property (retain) NSString *globalMessage;
+ (GlobalData*)sharedGlobalData;
+(void)globalFunction;

@end

GlobalData.m

#import "GlobalData.h"

@implementation GlobalData
@synthesize globalMessage;
static GlobalData *sharedGlobalData=nil;

+(GlobalData*)sharedGlobalData{
    if (sharedGlobalData==nil) {
        sharedGlobalData = [[super allocWithZone:NULL]init];
    }

    sharedGlobalData.globalMessage=@"Default Global Message";
    return sharedGlobalData;
    }

+(void)globalFunction{
    sharedGlobalData.globalMessage=@"New Global Message";
    NSLog(@"Set %@",sharedGlobalData.globalMessage);
}

@end

MyDocuments.h

#import <Cocoa/Cocoa.h>
#import "GlobalData.h"

@interface MyDocument : NSPersistentDocument {
}

@end

MyDocuments.m

#import "MyDocument.h"

@implementation MyDocument

- (id)init 
{
    self = [super init];

    NSLog(@"Outside class: '%@'",
          [GlobalData sharedGlobalData].globalMessage);
    NSLog(@"Outside class setting new message...");
    [GlobalData globalFunction];
    NSLog(@"Outside class: '%@'",
          [GlobalData sharedGlobalData].globalMessage);
    return self;
}

- (NSString *)windowNibName 
{
    return @"MyDocument";
}

@end

Image of output.

Was it helpful?

Solution

Changing sharedGlobalData in GlobalData.m to

+(GlobalData*)sharedGlobalData{
    if (sharedGlobalData==nil) {
        sharedGlobalData = [[super allocWithZone:NULL]init];
        sharedGlobalData.globalMessage=@"Default Global Message";
    }
    return sharedGlobalData;
}

has resolved my issue.

I'm not sure why, but sharedGlobalData was always called after globalFunction, and so my globalMessage would be immediately reset to it's initial definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top