1

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.

Mish
  • 31
  • 6
  • I just made my account so I couldn't post a second link! [Here's the link to the blog post.](http://sree.cc/iphone/global-functions-and-variables-in-objective-c) – Mish Sep 12 '13 at 17:03
  • 1
    Found my own answer... +(GlobalData*)sharedGlobalData was being called every time globalFunction was called, and so the globalMessage would be reset to default every time. Moved the initial definition of globalMessage into the if statement. Now everything works. – Mish Sep 12 '13 at 19:39
  • If you found the answer to your own question, you can post it as an answer rather than a comment and then accept it, in case someone else has a similar problem in the future. – Kevin DiTraglia Sep 12 '13 at 19:41
  • Sadly, I cannot post an answer to my own question for 8 hours as I'm new to the site and only have 1 reputation. I'll go ahead and do that tomorrow. – Mish Sep 12 '13 at 19:48
  • Oh I wasn't aware of that rule, I gave you an up-vote maybe that will help =) – Kevin DiTraglia Sep 12 '13 at 19:49
  • Thanks man! :-) 1 more up-vote and I can add it early haha! Takes 10 reputation. – Mish Sep 12 '13 at 19:50

1 Answers1

0

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.

Mish
  • 31
  • 6