Objective-C中的变量声明差异 [英] Variable declaration difference in Objective-C

查看:89
本文介绍了Objective-C中的变量声明差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关iOS 6中coreImage的教程. 在该教程中,我发现了类似的内容:

I'm was reading a tutorial about coreImage in iOS 6. In that tutorial I found something like:

@implementation ViewController {
    CIContext *context;
    CIFilter *filter;
    CIImage *beginImage;
    UIImageOrientation orientation;
}
//Some methods
@end

在.m文件中,在@implementation语句后的括号中声明变量.我是第一次看到这种变量声明.

The variables are declared in the .m file in a parenthesis after the @implementation statement. I'm seeing these kind of variable declaration first time.

上面的变量声明和下面的代码之间是否有任何区别

Is there any difference between the above variable declaration and the following code

@implementation ViewController

    CIContext *context;
    CIFilter *filter;
    CIImage *beginImage;
    UIImageOrientation orientation;

//Some methods

@end

推荐答案

有很大的不同.

@interface@implementation之后紧跟在方括号内的变量是实例变量.这些是与类的每个实例相关联的变量,因此可以在实例方法中的任何位置访问.

Variables inside brackets directly after the @interface or @implementation are instance variables. These are variables associated with each instance of your class, and thus accessible anywhere in your instance methods.

如果不放在方括号中,则声明全局变量.声明在任何括号块之外的任何变量将是全局变量,无论这些变量在@implementation指令之前还是之后.而且全局变量是邪恶的,需要不惜一切代价避免使用(可以声明全局常量,但要避免使用全局变量),尤其是因为它们不是线程安全的(因此可能会生成麻烦调试).

If you don't put the brackets, you declare global variables. Any variable declared outside of any bracket block will be a global variable, whether these variables are before or after the @implementation directive. And global variables are evil and need to be avoided at all costs (you can declare global constants, but avoid global variables), especially because they are not thread-safe (and may thus generate bugs that are a mess to debug).

实际上,从历史上看(在Objective-C的第一个版本和编译器中),您只能在.h文件中的@interface之后的括号中声明实例变量.

In fact, historically (in first versions of Objective-C and the compilers), you were only able to declare instance variables in brackets after your @interface in your .h file.

// .h
@interface YourClass : ParentClass
{
    // Declare instance variables here
    int ivar1;
}
// declare instance and class methods here, as well as properties (which are nothing more than getter/setter instance methods)
-(void)printIVar;
@end

// .m
int someGlobalVariable; // Global variable (bad idea!!)

@implementation YourClass

int someOtherGlobalVariable; // Still a bad idea
-(void)printIVar
{
    NSLog(@"ivar = %d", ivar1); // you can access ivar1 because it is an instance variable
    // Each instance of YourClass (created using [[YourClass alloc] init] will have its own value for ivar1
}

只有现代编译器允许您在类扩展(.m实现文件中的@interface YourClass ())或@implementation中声明实例变量(仍放在方括号中),此外还可以在.h中的@interface.好处是可以通过在.m文件中而不是在.h文件中声明它们来向类的外部用户隐藏这些实例变量,因为类的用户无需了解的内部编码详细信息您的班级,但只需要知道公共API.

Only modern compilers let you declare instance variables (still in brackets) also inside either your class extension (@interface YourClass () in your .m implementation file) or in your @implementation, in addition to the possibility to declare them after the @interface in your .h. The benefits being to hide those instance variables from external users of your classes, by declaring them in the .m file and not in the .h file anymore, because users of your class don't need to be aware of the internal coding details of your class, but only needs to know the public API.

最后一条建议:Apple越来越多地建议不要直接使用@property,而应使用编译器(明确地使用@synthesize指令,或者对于现代LLVM编译器是隐式的)来生成内部支持变量,而不是使用实例变量. .这样一来,您通常根本不需要声明实例变量,因此可以在@interface指令之后省略空的{ }:

One final advice: instead of using instance variables, Apple more and more recommends to use @property directly, and let the compiler (explicitely using the @synthesize directive, or implicity with modern LLVM compilers) generate the internal backing variable. So that at the end you generally won't need to declare instance variables at all, and thus omit the empty { } after the @interface directive:

// .h
@interface YourClass : ParentClass

// Declare methods and properties here
@property(nonatomic, assign) int prop1;
-(void)printProp;
@end

// .m
@implementation YourClass
// @synthesize prop1; // That's even not needed with modern LLVM compiler
-(void)printProp
{
    NSLog(@"ivar = %d", self.prop1);
}

这篇关于Objective-C中的变量声明差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆