我正在处理一本书中的代码清单,它有一对变量(特别是NSString *)在@实现中声明和初始化,而不是@接口,但在任何方法体之外。我以前没有见过这种情况,我想知道这在范围等方面有什么不同。
我在Objective C编程语言中快速查看了一下,但我看不到任何描述它的效果的东西。
谢谢
安迪
发布于 2010-06-17 20:43:23
在@implementation中声明的变量具有全局作用域。
如果您将它们声明为"static",则它们仅在同一源文件中的方法中可见。
所以:
@implementation MyClass
NSString *myString; // global scope, and accessible by all code in your project
或
@implementation MyClass
static NSString *myString; // global scope, but only accessible by code
// in this source file
https://stackoverflow.com/questions/3061652
复制相似问题