现在的位置: 首页 > 综合 > 正文

Static variable in Objective C

2014年01月16日 ⁄ 综合 ⁄ 共 2936字 ⁄ 字号 评论关闭

Ref: http://mobiledevelopertips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html

The concepts of “static” variables are different in Objective-C compared to Java. That does not mean, however, that the concepts do not exist.
Consider the following common uses of ‘static’ in Java:
  • public static final int MY_CONSTANT = 0; // Constant
  • public static String MyVar = “Foo”; // Class Variable
In all of the above cases, the use of the keyword ‘static’ is used to consistently refer to a variable that is class scoped and not instance scoped. In the case of a static variable, this means that multiple instances of the same
class would all share the state for the variable; Change the variable and it changes for all instances of that class.
Let’s take each of these cases and examine how they translate into Objective-C constructs …
Example: Using ‘static’ to define Constants
I often find myself using public static final qualifiers to define constants that are used throughout my code. A common declaration looks something like this :
1
2
3
public class MyClass {
  public static final int MY_CONSTANT = 0;
}
… and its usage would look something like this:
1
int i = MyClass.MY_CONSTANT;
There is no direct equivalent of this in Objective-C, but I do find that the use of #define will often provide an acceptable alternative. The following is how you would declare a constant using #define:
1
#define MY_CONSTANT 0
… and its usage in Objective-C would look something like this:
1
int i = MY_CONSTANT;
NOTE: Constructs like #define are called ‘macros’ in Objective-C and have many uses with no equivalent in Java. They are very powerful and can be used to do some fancy
conditional include/exclude of entire code blocks. I’d strongly recommend that you take a closer look at ‘macros’ in Objective-C to learn more.
Example: Using ‘static’ to Define Class Variables
Oddly enough, I found it very difficult to find out how to define a static variable in Objective-C. I suspect that must mean that its use may be discouraged for some reason by the creators of Objective-C. Personally I see no issue with them as long as they
are used safely. Obviously, race conditions and other common threading problems often result from the use of static variables but that can be easily avoided with careful use of synchronization and semaphores.
All that said, here is how you define a static variable in Objective-C:
1
2
3
4
5
6
7
8
9
10
11
12
13
@interface MyClass 
{
  // ...
}
+ (NSString *)myVar;
+ (void)setMyVar:(NSString *)newVa;
@end
 
@implementation MyClass
static NSString *myVar;
+ (NSString *)myVar { return myVar; }
+ (void)setMyVar:(NSString *)newVar { myVar = newVar; }
@end
NOTE: Just because the keyword ‘static’ is used, it doesn’t mean the same thing as it does in Java. In the above example, the static variable ‘myVar’ is only visible to code in the *.m file so if you want to access
the value externally, you will need to add static accessors (e.g. Class scoped methods declared with ‘+’ instead of ‘-’).
UPDATE: Another reader has pointed out that extern syntax in C can be used to expose a variable to anyone that imports the header file that defines it.
However, extern is not an alternative to a class-scoped static variable in Java, in fact it is more like a global variable … so use with caution. The process outlined in this blog post of wrapping a static C style variable with Objective-C style class-methods
is the closest equivalent you will find for a Java static variable.

抱歉!评论已关闭.