I want store a number as a global variable. What syntax do I use, and how can other parts of my application access that variable?
-
1This is your 3rd post in a row and is not the way you should be trying to get answers. Consider editing the original post or commenting on it to keep it active Please review the SO FAQJoe– Joe2011-08-16 16:13:03 +00:00Commented Aug 16, 2011 at 16:13
-
1possible duplicate of Storing and Accessing Methods from the AppDelegateAnomie– Anomie2011-08-16 16:18:53 +00:00Commented Aug 16, 2011 at 16:18
6 Answers
For a standard global variable (not persistent when the app is terminated and restarted) add this to a header file (*.h) of your choice:
extern NSInteger MYGlobalVariable;
Then put this in the implementation file; (*.m, *.c, *.cpp):
MYGlobalVariable = 0; // Or any other default value.
That is how you do a bread and butter global variable.
4 Comments
extern NSDictionary globalStuff in my AppDelegate, and initialised it in the AppDelegate's application:didFinishLaunchingWithOptions: method, but when I try to compile it, I get a linker error Undefined symbols for architecture i386. I also reference the dictionary in other classes. Could you provide some help?You probably want to use NSUserDefaults for this :
From anywhere in your code, you can set a value for a key :
int userAge = 21; // Just an example
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:[NSNumber numberWithInt:userAge] forKey:@"age"];
[standardUserDefaults synchronize];
}
And get it back from any other place :
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *age = nil;
if (standardUserDefaults)
age = [standardUserDefaults objectForKey:@"age"];
userAge = [age intValue]
You can also set an initial value :
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary
dictionaryWithObject:[NSNumber numberWithInt:13] forKey:@"age"];
[defaults registerDefaults:appDefaults];
Also, if you have complex data, you may want to create a wrapper class with setters and getters.
2 Comments
Define the variable in AppDelegate.h file. Create a property in .h file
@property (retain, nonatomic) NSString *str;
Then synthesize in AppDelegate.m file;
@synthesize str;
Later define a variable in you project prefix.pch file
#define DELEGATE ((AppDelegate*)[[UIApplication sharedApplication]delegate])
Use the value anywhere in your project
AppDelegate *a = DELEGATE;
a.str = @"value";
NSLog(@"value of variable str : %@",a.str);
2 Comments
To make a Variables that can be seen by the whole files in Objective c
for example you want to set your base url one time and in each class you append on it some extra strings,
go to main.m file, because this is the place the whole app will see it. then outside main function, put your base url
NSString *baseurl = @"staging.nabdanet.com";
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
and when you want to access these class, all what you will do is this,
SomeClass.m
extern NSString *baseurl;
with the same name in both ;)
2 Comments
To declare a global variable in either objective-C or Swift, you simply declare it outside the scope of any class/interface.
Objective-C:
#import "headerFile1.h"
#import "headerFile2.h"
BOOL isTrue = true;
int x = 1;
@interface exampleInterface (){
...
}
@end
@implementation exampleClass
...
isTrue= false; // can be used in the same way anyplace in your code
x=3; // anyplace means anyplace, even from other controllers
@end
Swift:
import UIKit
var x=45;
class SomeClass {
...
x=0; // This is also available from anyplace in your project
...
}