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

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

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

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

形式如下:

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

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

1 @property (assign, readonly) BOOL gameCenterAvailable;

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

结果可能是这样:

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推荐的,不用会有什么后果,我没试过,有兴趣的童鞋可以试一下。

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

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

IOS,objective_C中用@interface和 @property 方式声明变量的区别的更多相关文章

  1. 【转】 IOS,objective_C中用@interface和 @property 方式声明变量的区别

    原文: http://blog.csdn.net/ganlijianstyle/article/details/7924446 1.在  @interface :NSObject{} 的括号中,当然N ...

  2. 网站开发进阶(六)JSP两种声明变量的区别

    JSP两种声明变量的区别 在JSP中用两种声明变量的方法,一种是在<%! %>内,一种是在<% %>内.他们之间有什么区别呢?我们直接看一个JSP文件来理解. 代码如下: &l ...

  3. PHP 闭包获取外部变量和global关键字声明变量的区别

    最近在学习workerman的时候比较频繁的接触到回调函数,使用中经常会因为worker的使用方式不同,会用这两种不同的方式去调用外部的worker变量,这里就整理一下PHP闭包获取外部变量和glob ...

  4. var、let、const声明变量的区别

    let和var声明变量的区别:1.let所声明的变量只在let命令所在的代码块内有效.(块级作用域) for(let i=0;i<10;i++){ // ... } console.log(i) ...

  5. JSP两种声明变量的区别

    感谢大佬:https://blog.csdn.net/tiercel2008/article/details/11553899?utm_source=distribute.pc_relevant.no ...

  6. 关于Let和var声明变量的区别

    Let是ES6中添加进来的一个关键字,用于声明变量,其法与var声明变量相同,不同点在于其作用域(块级). 举例可以看出其具体差别 for(var i=0;i<5;i++){ console.l ...

  7. es6中的let声明变量与es5中的var声明变量的区别,局部变量与全局变量

    自己通过看typescript官方文档里的let声明,与阮一峰老师翻译的的es6学习文档,总结以下三点 1.var声明可以多次重复声明同一个变量,let不行 2.let变量只在块级作用域里面有效果,v ...

  8. ES6的let和var声明变量的区别

    关于let的描述 let允许你声明一个作用域被限制在块级中的变量.语句或者表达式.与var关键字不同的是,它声明的变量只能是全局或者整个函数块的. 作用域规则 let声明的变量只在其声明的块或子块中可 ...

  9. ES6之用let,const和用var来声明变量的区别

    var(掌握) 不区分变量和常量   用var声明的变量都是变量,都是可变的,我们可以随便对它进行运算操作.这样当多个人进行同一个项目时,区分变量和常量会越来越难,一不小心就会把设计为常量的数据更改了 ...

随机推荐

  1. 【web.xml】报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    今天搭建新的项目,虽然在web.xml中配置了ContextLoaderListener以及IntrospectorCleanupListener 如下: web.xml中部分代码: <!-- ...

  2. jq:zclip复制

    实例: <script type="text/javascript" src="js/jquery.js"></script> < ...

  3. [Linux] ubuntu 软件安装必须看的网址

    http://wiki.ubuntu.org.cn/index.php?title=Qref/Apps&variant=zh-hans 这里介绍了unbuntu常用软件及其安装,免得你百度来百 ...

  4. linux:ls、ls -l、ls -al区别 示例

    linux:ls.ls -l.ls -al区别 示例 比如test文件夹下有一个test文件.一个.文件夹.一个..文件夹. 则,执行三个命令后,显示效果如下: [root@linuxserver t ...

  5. C# 深入理解堆栈、堆在内存中的实现

    尽管在.NET framework下我们并不需要担心内存管理和垃圾回收(GarbageCollection),但是我们还是应该了解它们,以优化我们的应用程序.同时,还需要具备一些基础的内存管理工作机制 ...

  6. 赵雅智_service生命周期

    Android中的服务和windows中的服务是类似的东西,服务一般没实用户操作界面.它执行于系统中不easy被用户发觉,能够使用它开发如监控之类的程序. 服务的开发步骤 第一步:继承Service类 ...

  7. openerp js调用Python类方法

    转自:http://blog.csdn.net/kuaileboy1989/article/details/42875497 js调用.py文件中定义的类 形式如下: //创建product.prod ...

  8. js实现页面元素随着内容的滚动而滚动

      CreateTime--2017年9月4日16:55:06 Author:Marydon js实现页面元素随着内容的滚动而滚动 分析: CSS样式,使用绝对定位确定好页面元素在屏幕的位置(如:正中 ...

  9. 【SSH进阶之路】Struts基本原理 + 实现简单登录(二)

    上面博文,主要简单的介绍了一下SSH的基本概念,比較宏观,作为刚開始学习的人可以有一个总体上的认识,个人觉得对学习有非常好的辅助功能.它不不过一个"瞭望塔".更是检验是否真正掌握全 ...

  10. 07-hibernate进阶

    1,hibernate.cfg.xml常用配置 2,session简介 3,transaction简介 4,session详解 5,对象关系映射常用配置 hibernate.cfg.xml常用配置 s ...