ObjectiveC中变量位置的声明/定义? [英] Declaration/definition of variables locations in ObjectiveC?

查看:181
本文介绍了ObjectiveC中变量位置的声明/定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从开始研究iOS应用程序和目标C以来,我一直对可以声明和定义变量的不同位置感到困惑。一方面我们采用传统的C方法,另一方面我们有新的ObjectiveC指令,可以在其上添加OO。你能不能帮助我了解最佳实践和情况,我希望将这些位置用于我的变量,或者纠正我目前的理解?

Ever since starting to work on iOS apps and objective C I've been really puzzled by the different locations where one could be declaring and defining variables. On one hand we have the traditional C approach, on the other we have the new ObjectiveC directives that add OO on top of that. Could you folks helps me understand the best practice and situations where I'd want to use these locations for my variables and perhaps correct my present understanding?

这是一个示例类( .h和.m):

Here's a sample class (.h and .m):

#import <Foundation/Foundation.h>

// 1) What do I declare here?

@interface SampleClass : NSObject
{
    // 2) ivar declarations
    // Pretty much never used?
}

// 3) class-specific method / property declarations

@end

#import "SampleClass.h"

// 4) what goes here?

@interface SampleClass()

// 5) private interface, can define private methods and properties here

@end

@implementation SampleClass
{
    // 6) define ivars
}

// 7) define methods and synthesize properties from both public and private
//    interfaces

@end




  • 我对1和4的理解是那些是C风格的基于文件的声明和定义,它们对类的概念一无所知,因此必须准确地使用它们将如何在C中使用。我见过它们之前用于实现基于静态变量的单例。我还缺少其他方便的用途吗?

  • 我对使用iOS的看法是,在@synthesize指令之外,ivars已被完全淘汰,因此可能会被忽略。是这样的吗?

  • 关于5:为什么我要在私有接口中声明方法?我的私有类方法似乎在没有接口声明的情况下编译得很好。它主要是为了可读性吗?

  • 非常感谢大家!伙计们!

    Thanks a bunch, folks!

    推荐答案

    我能理解你的困惑。特别是因为最近对Xcode和新LLVM编译器的更新改变了ivars和属性的声明方式。

    I can understand your confusion. Especially since recent updates to Xcode and the new LLVM compiler changed the way ivars and properties can be declared.

    在现代之前Objective-C(在旧对象中) C 2.0)你没有很多选择。实例变量曾在大括号 {} 之间的标题中声明:

    Before "modern" Objective-C (in "old" Obj-C 2.0) you didn't have a lot of choices. Instance variables used to be declared in the header between the curly brackets { }:

    // MyClass.h
    @interface MyClass : NSObject {
        int myVar;
    }
    @end
    

    您只能在您的访问中访问这些变量实现,但不是来自其他类。要做到这一点,你必须声明访问器方法,看起来像这样:

    You were able to access these variables only in your implementation, but not from other classes. To do that, you had to declare accessor methods, that look something like this:

    // MyClass.h
    @interface MyClass : NSObject {
        int myVar;
    }
    
    - (int)myVar;
    - (void)setMyVar:(int)newVar;
    
    @end
    
    
    // MyClass.m
    @implementation MyClass
    
    - (int)myVar {
       return myVar;
    }
    
    - (void)setMyVar:(int)newVar {
       if (newVar != myVar) {
          myVar = newVar;
       }
    }
    
    @end
    

    这个你可以从其他类中获取和设置这个实例变量的方式,使用通常的方括号语法来发送消息(调用方法):

    This way you were able to get and set this instance variable from other classes too, using the usual square bracket syntax to send messages (call methods):

    // OtherClass.m
    int v = [myClass myVar];  // assuming myClass is an object of type MyClass.
    [myClass setMyVar:v+1];
    

    因为手动声明并实现每个访问器方法非常烦人, @property <引入/ code>和 @synthesize 以自动生成访问方法:

    Because manually declaring and implementing every accessor method was quite annoying, @property and @synthesize were introduced to automatically generate the accessor methods:

    // MyClass.h
    @interface MyClass : NSObject {
        int myVar;
    }
    @property (nonatomic) int myVar;
    @end
    
    // MyClass.m
    @implementation MyClass
    @synthesize myVar;
    @end
    

    结果更清晰,代码更短。将为您实现访问器方法,您仍然可以像以前一样使用括号语法。但此外,您还可以使用点语法访问属性:

    The result is much clearer and shorter code. The accessor methods will be implemented for you and you can still use the bracket syntax as before. But in addition, you can also use the dot syntax to access properties:

    // OtherClass.m
    int v = myClass.myVar;   // assuming myClass is an object of type MyClass.
    myClass.myVar = v+1;
    

    从Xcode 4.4开始,你不必再自己声明一个实例变量了,你可以跳过 @synthesize 也是。如果你没有声明一个ivar,编译器会为你添加它,它也会生成访问器方法,而你不必使用 @synthesize

    Since Xcode 4.4 you don't have to declare an instance variable yourself anymore and you can skip @synthesize too. If you don't declare an ivar, the compiler will add it for you and it will also generate the accessor methods without you having to use @synthesize.

    自动生成的ivar的默认名称是以下划线开头的名称或属性。您可以使用 @synthesize myVar = iVarName更改生成的ivar名称;

    The default name for the automatically generated ivar is the name or your property starting with an underscore. You can change the generated ivar's name by using @synthesize myVar = iVarName;

    // MyClass.h
    @interface MyClass : NSObject 
    @property (nonatomic) int myVar;
    @end
    
    // MyClass.m
    @implementation MyClass
    @end
    

    这与上面的代码完全相同。出于兼容性原因,您仍然可以在标头中声明ivars。但是因为您想要这样做(而不是声明属性)的唯一原因是创建一个私有变量,现在您也可以在实现文件中执行此操作,这是首选方法。

    This will work exactly as the code above. For compatibility reasons you can still declare ivars in the header. But because the only reason why you would want to do that (and not declare a property) is to create a private variable, you can now do that in the implementation file as well and this is the preferred way.

    实现文件中的 @interface 块实际上是 Extension ,可用于转发声明方法(不再需要)和(重新)声明属性。例如,您可以在标题中声明 readonly 属性。

    An @interface block in the implementation file is actually an Extension and can be used to forward declare methods (not needed anymore) and to (re)declare properties. You could for instance declare a readonly property in your header.

    @property (nonatomic, readonly) myReadOnlyVar;
    

    并在您的实施文件中重新声明它为 readwrite 能够使用属性语法设置它,而不仅仅是通过直接访问ivar。

    and redeclare it in your implementation file as readwrite to be able to set it using the property syntax and not only via direct access to the ivar.

    至于完全在任何之外声明变量@interface @implementation 阻止,是的,这些是普通的C变量并且工作完全相同。

    As for declaring variables completely outside of any @interface or @implementation block, yes those are plain C variables and work exactly the same.

    这篇关于ObjectiveC中变量位置的声明/定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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