How to define a global variable that can be accessed anywhere in my application? [duplicate]
Possible Duplicate:
Global开发者_Go百科 int variable objective c
I would like to create a global variable. I want to access to this variable anywhere.
The Java equivalent:
static var score:int = 0;
For example if I define a global variables into the Game class. How to access to this global variable?
Game.score ?
If you are having multiple views in your application, and in that case you want to have a variable accessible to every view, you should always create a Model/Data class and define the variable in it. Something like this :
Objective-C :
//DataClass.h
@interface DataClass : NSObject {
NSString *str;
}
@property(nonatomic,retain)NSString *str;
+(DataClass*)getInstance;
@end
//DataClass.m
@implementation DataClass
@synthesize str;
static DataClass *instance = nil;
+(DataClass *)getInstance
{
@synchronized(self)
{
if(instance==nil)
{
instance= [DataClass new];
}
}
return instance;
}
Now in your view controller you need to call this method as :
DataClass *obj=[DataClass getInstance];
obj.str= @"I am Global variable";
This variable will be accessible to every view controller. You just have to create an instance of Data class.
Swift :
class DataClass {
private var str: String!
class var sharedManager: DataClass {
struct Static {
static let instance = DataClass()
}
return Static.instance
}
}
Usage : DataClass.sharedManager.str
Using dispatch_once
class DataClass {
private var str: String!
class var sharedInstance: DataClass {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: DataClass? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = DataClass()
}
return Static.instance!
}
}
Usage : DataClass.sharedManager.str
Objective-C does not have support for "class variables" directly. Instead, you can create a variable which is valid for the scope of the class's file and access it using class methods.
// outside the implementation
static int score = 0; // static means it is only accessible from the current file
@implementation Game
+ (int)score {
return score;
}
+ (void)setScore:(int)newScore {
score = newScore;
}
The preferred way to implement global variables in an iOS project (though these aren't true global variables), is to create a property in the application delegate, then just access that property from each of your classes.
EDIT: Re-reading your question, it looks like I misinterpreted your question, and ughoavgfhw's answer is probably what you're looking for. He's correct, there is no such thing as a class variable in Objective-C, so you have to create a regular C static variable, then create class methods (denoted by the +
rather than a -
) for setting and getting.
Though generally, when I need "global" variables in an app, I create singleton classes to house them and their related methods (so the app delegate doesn't overflow with unrelated properties), or if it's a smaller project I just use the application delegate (which is a also a singleton class) rather than separate singletons. Though there's nothing wrong with the static variable plus class setter/getter approach if that works better for your needs.
精彩评论