@implementation 中声明的变量
Posted
技术标签:
【中文标题】@implementation 中声明的变量【英文标题】:variables declared in @implementation 【发布时间】:2010-06-17 12:41:32 【问题描述】:我正在研究一本书的代码清单,它有一对变量(特别是 NSString *)在@implementation 而不是@interface 中声明和初始化,但在任何方法主体之外。我以前没有见过这个,我想知道这在范围等方面有什么不同。
我快速浏览了 The Objective C Programming Language,但我看不到任何描述它有什么效果的东西。
谢谢
安迪
【问题讨论】:
你会发布代码示例来显示你在说什么吗? 【参考方案1】:在@implementation 中声明的变量具有全局范围。
如果您将它们声明为“静态”,则它们仅在同一源文件中的方法中可见。
所以:
@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
【讨论】:
我在寻找答案并意识到如果它在手镯中,那么它实际上是 ivar。见***.com/questions/6785765/… 你确定吗??我不能让它工作。也许你做了错误的 cmets 在@implementation
块之外或内部定义它们有什么区别?以上是关于@implementation 中声明的变量的主要内容,如果未能解决你的问题,请参考以下文章