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?
问题:
回答1:
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.
回答2:
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.
回答3:
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);
回答4:
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 ;)
回答5:
For persistent vars, use NSUserDefaults. This gets written on a file in the app sandbox. For session vars (non-persistent), I use a singleton class with an NSMutableDictionary property to store variables.
回答6:
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
...
}