Objective-C中的属性和实例变量

首先、引用一篇关于@property与@synthesize的老文。交代一下这两个属性的由来、便于下文理解

——————————————————————————————————————

    当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property和@synthesize属性,@property用在 .h 头文件中用作声明,@synthesize用在.m 文件中用于实现。

如下,新建一个基于“Command Line Tool”的项目,名为“property”,再新建一个Student类,

传统的写法是:

Student.h

  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //age的getter和setter方法声明  
  18. - (int)age;  
  19. - (void)setAge:(int)newAge;  
  20.   
  21. //no的getter和setter方法声明  
  22. - (int)no;  
  23. - (void)setNo:(int)newNo;  
  24.   
  25. @end  
Student.m

  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //age的getter和setter方法的实现  
  14. - (int)age  
  15. {  
  16.     return age;  
  17. }  
  18. -(void)setAge:(int)newAge  
  19. {  
  20.     age = newAge;  
  21. }  
  22.   
  23. //no的getter和setter方法的实现  
  24. - (int)no  
  25. {  
  26.     return no;  
  27. }  
  28. - (void)setNo:(int)newNo  
  29. {  
  30.     no = newNo;  
  31. }  
  32.   
  33. @end  
main.m

  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"   
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.   
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;//这句相当于setter方法  
  20.         NSLog(@"age is %i", stu.age);//这里的 stu.age 相当于getter方法  
  21.           
  22.         [stu release];  
  23.           
  24.     }  
  25.     return 0;  
  26. }  

------------------------------------------------------------------------------------------------------------------------

用@property和@synthesize的写法是:

 Student.h

  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //当编译器遇到@property时,会自动展开成getter和setter的声明  
  18. @property int age;  
  19. @property int no;  
  20.   
  21.   
  22. @end  

Student.m

  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //@synthesize 会自动生成getter和setter的实现  
  14. //@synthesize 默认会去访问age,no,height同名的变量,,  
  15. //如果找不到同名的变量,会在内部自动生成一个私有同名变量age,no,height,,  
  16. //因此Student.h 中的这几个变量也可以省略不写。  
  17. @synthesize age,no;  
  18.   
  19. @end  

main.m

  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.       
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;  
  20.         NSLog(@"age is %i", stu.age);  
  21.           
  22.         [stu release];  
  23.     }  
  24.     return 0;  
  25. }  

在Xcode4.5及以后的版本中,可以省略@synthesize ,编译器会自动帮你加上getter 和 setter 方法的实现,并且默认会去访问

_age这个成员变量,如果找不到_age这个成员变量,会自动生成一个叫做 _age私有成员变量。


———————————————————————————————————————————————————————————————————————————————————————————————————————————————————

OK进入正题

Objective-C中先有的实例变量,需要给外部类使用的用@public声明,内部自己使用的用@private或@protect声明。Objective-C添加了属性后,属性用于对外而实例变量主要用于程序内部使用。这样有利于代码的分离,由于编译器会直接给属性提供对应的实例变量(当然也可以手动指定该属性所对应的实例变量)和getter/setter方法。

类Class中的属性property

在老版本的Objective-C语言中,我们需要同时声明属性和底层实例变量,那时,属性是Objective-C语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface MyViewController :UIViewController{  
  2.   
  3. __strong UIButton *_myButton;  
  4.   
  5. }  
  6.   
  7. @property (nonatomicretainUIButton *myButton;  
  8.   
  9. @end  

后来,苹果将默认编译器从GCC转换为LLVM(low level virtual machine),从此不再需要为属性声明实例变量了。如果LLVM发现一个没有匹配实例变量的属性,它将自动创建一个以下划线开头的实例变量。因此,在这个版本中,我们不再为输出口声明实例变量。

例如:MyViewController.h文件

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface MyViewController :UIViewController  
  2.   
  3. @property (nonatomicretainUIButton *myButton;  
  4.   
  5. @end  

在MyViewController.m文件中,编译器也会自动的生成一个实例变量_myButton。那么在.m文件中可以直接的使用_myButton实例变量,也可以通过属性self.myButton.都是一样的。

注意这里的self.myButton其实是调用的myButton属性的getter/setter方法。这与C++中点的使用是有区别的,C++中的点可以直接访问成员变量(也就是实例变量)。

例如在Objective-C中有如下代码

.h文件

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface MyViewController :UIViewController{  
  2.   
  3. __strong NSString *_name;  
  4.   
  5. }  
  6.   
  7. @end  

.m文件中,self.name 这样的表达式是错误的。xcode会提示你使用->,改成self->name就可以了。因为oc中点表达式是表示调用方法,而上面的代码中没有name这个方法。

oc语法关于点表达式的说明:”点表达式(.)看起来与C语言中的结构体访问以及java语言汇总的对象访问有点类似,其实这是oc的设计人员有意为之。如果点表达式出现在等号 = 左边,该属性名称的setter方法将被调用。如果点表达式出现在右边,该属性名称的getter方法将被调用。”

所以在oc中点表达式其实就是调用对象的setter和getter方法的一种快捷方式, 例如:dealie.blah = greeble 完全等价于 [dealie.blah setBlah:greeble];

以前的用法,声明属性跟与之对应的实例变量:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface MyViewController :UIViewControlle{  
  2.   
  3. __strong UIButton *_myButton;  
  4.   
  5. }  
  6.   
  7. @property (nonatomicretainUIButton *myButton;  
  8.   
  9. @end  

这种方法基本上使用最多,现在大部分也是在使用,因为很多开源的代码都是这种方式。但是ios5更新之后,苹果是建议以以下的方式来使用:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface MyViewController :UIViewController  
  2.   
  3. @property (nonatomicretainUIButton *myButton;  
  4.   
  5. @end  

因为编译器会自动为你生成以下划线开头的实例变量_myButton,不需要自己手动再去写实例变量。而且也不需要在.m文件中写@synthesize myButton;也会自动为你生成setter,getter方法。@synthesize的作用就是让编译器为你自动生成setter与getter方法

@synthesize 还有一个作用,可以指定与属性对应的实例变量,例如@synthesize myButton = xxx;那么self.myButton其实是操作的实例变量xxx,而不是_myButton了。

在实际的项目中,我们一般这么写.m文件

@synthesize myButton;

这样写了之后,那么编译器会自动生成myButton的实例变量,以及相应的getter和setter方法。注意:_myButton这个实例变量是不存在的,因为自动生成的实例变量为myButton而不是_myButton,所以现在@synthesize的作用就相当于指定实例变量;

如果.m文件中写了@synthesize myButton;那么生成的实例变量就是myButton;如果没写@synthesize myButton;那么生成的实例变量就是_myButton。所以跟以前的用法还是有点细微的区别。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值