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

摘要:
表格如下:1@interfaceGCTurnBasedMatchHelper:NSObject{2BOOLgameCenterAvailable;3BOLuserAuthenticated;4}2。另一种方法是直接在@interface:NSObject}括号后面定义带有@property的变量。1@propertyBOOLgameCenterAvailable ; 您会发现,在@interface中定义变量后,有人在@property中重复定义同一变量是非常常见的。结果可能是:1@interfaceGCTurnBasedMatchHelper:NSObject{2BOOLgameCenterAvailable;3BOLuserAuthenticated;4}56@propertyBOOLgameCenterAvailable ; 此外,您可以在@interface中独立地定义变量,而不是@property定义;您也只能使用@property来定义,而不能在@interface中定义。当然,您可以使用@property来定义。通常,需要在中使用@synchsize。m文件来合成相应的setter和getter方法。

转帖请在开头处注明文章链接,请支持原创。

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

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

      1.在  @interface :NSObject{} 的括号中,当然NSObject 是指一个父类,可以是其他的。

      形式如下:

1@interface GCTurnBasedMatchHelper : NSObject {

2BOOL gameCenterAvailable;

3BOOL userAuthenticated;

4 }

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

1 @property (assign, readonly) BOOL gameCenterAvailable;

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

     结果可能是这样:

 

1@interface GCTurnBasedMatchHelper : NSObject {

2BOOL gameCenterAvailable;

3BOOL userAuthenticated;

4}

5 

6 @property (assign, readonly) BOOL gameCenterAvailable;

 

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

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

    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的特色,结果却带来这么个麻烦。

免责声明:文章转载自《objective_C中用@interface和 @property 方式声明变量的区别》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇如何基于nand flash启动Linux内核(分享一段实用、简单、类似bootloader功能的代码)ovn-kubernetes安装指南下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#中out的用法

out的用法out关键字会导致参数通过引用来传递。这与ref关键字类似,不同之处在于ref要求变量必须在传递之前进行初始化。若要使用out参数,方法定义和调用方法都必须显式使用out关键字。例如: classOutExample { static void Method(out inti) { i = 44; }...

Delphi ini文件读写

参考:http://www.cnblogs.com/zhangzhifeng/archive/2011/12/01/2270267.html 一、ini文件的结构 ;这是关于 ini 文件的注释 [节点] 关键字=值 ... ini文件允许有多个节点,每个节点有允许有多个关键字,“=”后面是该关键字的值(类型有三种:字符串、整型数值和布尔值。其中字符串存...

VBA学习_1:数据类型

VBA的数据类型 布尔型Boolean 整数:整数型Integer、字节型Byte、长整数型Long 小数:小数型Decimal、单精度浮点型Single、双精度浮点型Double、货币型Currency 字符串型Sting(定长和不定长) 日期型Date 对象型Object 变体型Variant 用户自定义类型 声明变量   Dim 变量名 As 数...

sqlserver的存储过程

继上一个数据库分页功能完成之后,这周开始进行一个sqlserver数据库的存储过程查询和运用。之前只了解过mysql数据库和oracle数据库,对于存储过程的了解只是在oracle数据库上面有所运用。加上对sqlserver数据库不太了解,所以一开始有点无从下手,看了一些资料。了解存储过程实际上就是将一些查询语句与数据库的事务处理相结合,方便我们在运用的时...

java环境变量配置(转)

java环境变量配置 windows xp下配置JDK环境变量:       1.安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为D:javajdk1.5.0_08;   2.安装完成后,右击“我的电脑”,点击“属性”;       3.选择“高级”选项卡,点击“环境变量”;       4.在“系统变量”中,设置...

C语言探索之旅 | 第一部分第五课:变量的世界(二),变量声明

作者 谢恩铭,公众号「程序员联盟」。 转载请注明出处。 原文:https://www.jianshu.com/p/8db33987cb49 《C语言探索之旅》全系列 内容简介 变量是什么? 给变量起个名字 变量的类型 声明变量 const 关键字 第一部分第六课预告 1. 变量是什么? 上一课我们学习的是 C语言探索之旅 | 第一部分第四课:变量...