IOS,objective_C中用@interface和 @property 方式声明变量的区别

转自http://www.cnblogs.com/letmefly/archive/2012/07/20/2601338.html html

 一直有疑问,在objective_C中声明变量会有 2种方式,今天有空和网友讨论了下,而且本身查了stackoverflew后算是稍微弄懂了一点。记录以下: ios

      用了一段oc;会发现有2种定义变量的方式 app

      1.在  @interface :NSObject{} 的括号中,固然NSObject 是指一个父类,能够是其余的。 函数

      形式以下: ui

1 @interface GCTurnBasedMatchHelper : NSObject { 
    BOOL gameCenterAvailable; 3 BOOL userAuthenticated; 
}

  2.另一种是直接在 @interface : NSObject{}括号以后,用 @property 去定义一个变量。 this

1 @property (assign, readonly) BOOL gameCenterAvailable;

  你会发现,有人会再@interface中定义了变量后,又在 @property中重复定义相同的变量,并且很常见。 spa

     结果多是这样: .net

复制代码
1 @interface GCTurnBasedMatchHelper : NSObject { 
2 BOOL gameCenterAvailable; 
3 BOOL userAuthenticated; 
4
5 
6 @property (assign, readonly) BOOL gameCenterAvailable;
复制代码

  并且你能够单独在@interface中定义变量,而不用@property定义;也能够只用@property去定义,而不在@interface中定义,固然用了@property去定义,通常要在.m文件中用@synthsize去合成相应的setter,getter方法。不然会获得一个警告。固然@synthsize是可选的,可是是Apple推荐的,不用会有什么后果,我没试过,有兴趣的童鞋能够试一下。 code

     那这两种方式有什么区别呢。 htm

    1. 只在@interface中定义变量的话,你所定义的变量只能在当前的类中访问,在其余类中是访问不了的;而用@property声明的变量能够在外部访问。

    2.用了@property去声明的变量,能够使用“self.变量名”的方式去读写变量。而用@interface的方式就不能够。

    3.  这里给出一个连接:http://stackoverflow.com/questions/9702258/difference-between-properties-and-variables-in-ios-header-file    里面讲到:  我英语菜,简单翻一下:

Defining the variables in the brackets simply declares them instance variables.

在括号中定义一个变量只是简单的声明了一个实例变量(实例变量应该指的成员变量)。  博主注:老外对variable 和instance variable是有不一样理解的。因此下文中 用了一个模糊的词 ivar。

Declaring (and synthesizing) a property generates getters and setters for the instance variable, according to the criteria within the parenthesis. This is particularly important in Objective-C because it is often by way of getters and setters that memory is managed (e.g., when a value is assigned to an ivar, it is by way of the setter that the object assigned is retained and ultimately released). Beyond a memory management strategy, the practice also promotes encapsulation and reduces the amount of trivial code that would otherwise be required.

声明(和 @synthsize)一个属性会为成员变量生成 getter 和setter方法,根据括号内的标准,在oc中常常用setter和getter 作内存管理,这是很重要的。(例如: 当一个值被赋给这个变量,对象是经过setter函数去分配,修改计数器,并最后释放的)。更高一个层次来讲,这种作法也促进了封装,减小了一些没必要要的代码。

It is very common to declare an ivar in brackets and then an associated property (as in your example), but that isn't strictly necessary. Defining the property and synthesizing is all that's required, because synthesizing the property implicitly also creates an ivar.

在@interface括号中定义一个变量并用@property 重复定义一次是很广泛的,实际上不是必要的。用@property和@synthszie就够了,由于在用@synthsize合成这个属性的读写方法时就会建立一个变量。

The approach currently suggested by Apple (in templates) is:

目前苹果(在模板中)建议的方法是这样的:

-Define property in header file, e.g.:

先在头文件中定义一个属性

1 @property int gameCenter;

Then synthesize & declare ivar in implementation:

而后在实现文件中  synthsize和declare成这样:

1 @synthesize gameCenter = __ gameCenter;

The last line synthesizes the gameCenter property and asserts that whatever value is assigned to the property will be stored in the __gameCenter ivar. Again, this isn't necessary, but by defining the ivar next to the synthesizer, you are reducing the locations where you have to type the name of the ivar while still explicitly naming it.

最后一行synthsize  gameCenter 属性并说明了无论什么值被分配给这个属性,都会存储到_gameCenter这个变量中。 再次说明,这不是必要的,可是,这样写了以后,你能减小输入已经明确命名的变量名。

  最后一句的意思you are reducing the locations where you have to type the name of the ivar while still explicitly naming it .很差翻。

   据千锋的第2节语法课课程的讲解,这样写以后能够使得 @synthsize 时内部getter方法会展成

1 -(int)gameCenter
2
3 return _gameCenter;
4 }

 而直接写  @synthsize  gameCenter;

setter函数会在内部展开成

1 -(int)gameCenter 
2
3 return gameCenter; 
4 }

  注意到:函数名和变量名是同样的。在斯坦福的课程中,白胡子教授也模糊的说道这样的同名有可能带来bug,具体什么bug他没说,我也没见过,因此仍是养成这样写的习惯为好。其余语言的getter函数  通常会在变量前加 get;但oc没有,多是为了与其余语言作区分,算是oc的特点,结果却带来这么个麻烦。

相关文章
相关标签/搜索