rustylepord
rustylepord

Reputation: 5801

Access static variable from different files

This should be simple but I am scratching my head on this. I have declared a static variable on the app delegate header file. Something similar to,

static NSString *baseURL = @"http://www.google.com/";

I change the value of the static variable during the method,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

When I observe the variable using the breakpoint in AppDelegate file , I can see that the value is changed. However when I access the static variable from a different file aka a ViewController I get the old value . Why is this ?

Is there any way I can get the new value ?

Upvotes: 0

Views: 131

Answers (2)

Swati
Swati

Reputation: 2918

I would suggest you to

  1. Declare an NSString property in your AppDelegate.h file
  2. Synthesize in AppDelegate.m
  3. Inside didFinishLaunchingWithOptions: set your variables value
  4. To access the value use this across the app:

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; appDelegate.baseURL = @"google.com";

Upvotes: 2

malex
malex

Reputation: 10096

Yo should not use static if you are going to change values.

To get access to "global" strings you need to use extern:

appDelegate.h

extern NSString *externString;


appDelegate.m

NSString *externString = @"some value";

Also you can change the value anywhere.

Upvotes: 4

Related Questions