Static variable in Objective C
来源:互联网 发布:明星衣橱软件下载 编辑:程序博客网 时间:2024/06/05 17:45
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 :… and its usage would look something like this:
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:
… and its usage in Objective-C would look something like this:
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:
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.- Static variable in Objective C
- static variable in method
- Objective-C categories in static library
- Objective-C categories in static library
- objective-c 之 static
- Static Variable, Auto Variable, Global Variable, Local Variable, Register Variable (C)
- static member variable and static member function in a class
- Objective-C property和instance variable
- Objective-C 之Static Analysis
- Objective-C中的static用法
- Objective-C static 类方法
- static variable,
- Static Variable
- JSON in objective c
- Protocol in Objective-C
- selector in Objective-C
- NS_BLOCK_ASSERTIONS in Objective-C
- Categories in Objective-C
- hdu2047阿牛的EOF牛肉串
- Porting Over iPad game to iphone the easy way cocos2d
- C++易迷惑的基础知识(1)
- NSURLRequest的官方文档
- 新手必读;优秀驾驶员开车技巧!!!
- Static variable in Objective C
- About @private, @public, @protected
- 程序员的十层境界
- 引用 google黑客技术基础
- 百万用户级游戏服务器架构介绍
- [LeetCode] Set Matrix Zeroes
- 深入理解iPhone数据持久化
- keil uVersion4+JLINKV8调试STM32单片机
- 输出1000以内的所有素数 回文数 回文素数 可逆素数