-1

I want to use global variables in my code. I have some ViewController that use some instances, so use global will be easier then pass instances between controllers.

Thus, I create the global in AppDelegate:

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
    //Global Variables
    NSInteger userID;
    NSMutableArray *friends;
}
@property (assign, nonatomic) NSInteger userID;
@property (strong, nonatomic) NSMutableArray *friends;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    self.friends = [[NSMutableArray alloc] init];
    return YES;

}

And I access global in any controller adding this code in ViewControllerN.h:

#import "AppDelegate.h"
#define global \ ((AppDelegate *)[UIApplication sharedApplication].delegate)

So in ViewControllerN.m I can access global, for example:

[global.friends addObject:@"Henry"];

It works perfectly. But in somewhere, I am losing data from global.

I start my app in PageControllerA (ControllerA)

  • Controller A have a scrollView with ControllerB,C and D.
  • I add data to global.friend in ControllerA viewdidLoad
  • Controller B,C,D can access global correctly in viewDidLoad, but when I call a method from any of this controllers global.friend is Empty.
  • Controller B call Controller E but global.friend is empty for Controller E too.

I only white data in global.friend, never remove any objects.

Why I am losing data? (global.friend empty)

rcmstark
  • 1,031
  • 1
  • 14
  • 18
  • 1
    A better question is why are you using global variables? Go against standard, Apple defined best practice and of course you will run into problems. Declare the variables static, and this may get solve your problem. [Or try this approach](http://stackoverflow.com/q/338195/874257) – Josiah Hester Oct 13 '12 at 04:04
  • 1
    Those aren't global variables. Those are App Delegate (singleton) instance variables. – hotpaw2 Oct 13 '12 at 05:10
  • Did you allocate your mutable array before trying to add objects to it? Check the order of method calls. – hotpaw2 Oct 13 '12 at 05:11
  • I am using Global because I have 5 Controller that use the same variable. So, to not duplicate data or pass data using parentController... and some controllers don't have direct connection with others – rcmstark Oct 13 '12 at 05:15

1 Answers1

0

As suggested by Josiah in one of the answers here there are better ways to do this. However, I like using Global Variables as well some times and I usually create an NSObject class and declare the global variables there.

To do so you need to create a new NSObject file. in the *.h file, above the interface you should declare your variable using 'extern'

extern NSMutableDictionary *SavedUsersDictionary;

in the *.m file you should your variable above the interface without the 'extern'

NSMutableDictionary *SavedUsersDictionary;

Then import the class wherever you need to use the variable. You can also set the variable as a class property and synthesize it.

Hope that helps.

Shachar
  • 1,110
  • 9
  • 22