iOS声明变量详解【转】

内容概述:

       本文主要讲述了ios中多种声明变量方式的区别与联系,以及@interface声明的成员变量与@property属性的差异。最后介绍了推荐的声明方式。


一、@interface @property的区别

前言:

1)@interface大括号中声明的是“成员变量”;

2)@property声明的是属性 @synthesize@property配对,意义是合成”。


成员变量与属性的区别主要分为以下两点:

1、在@interface中定义变量的话,为当前类的私有(private),顾名思义,这些变量只能在当前类中被访问;而用@property声明的变量为公有(public),可以在当前类或者其他类中被访问。

2、使用@interface声明的变量,使用变量名进行访问;@property声明的变量用“ _变量名”(不用@synthesize的方式,后面会提及),或者“self.变量名”的形式进行访问。


二、多种变量声明方式

让我们看看下面的几种变量声明、以及调用方式:

1)在@interface中声明ivar(实例变量),即成员变量,上文已经提及过,这种方式声明的变量是私有的。

声明:在此声明一个NSString的变量atany。

[cpp]  view plain copy
  1. @interface MethodOne : NSObject{  
  2.     NSString *atany;  
  3. }  
调用 :在.m文件中直接使用变量名调用即可。

[cpp]  view plain copy
  1. NSLog(@"hello %@",atany);  


2)不在.h中声明变量,而直接在.m的@implementation中声明变量。

声明

[cpp]  view plain copy
  1. @implementation MethodOne{  
  2.     NSString *atany;  
  3. }  

调用:这种声明方式与1)大同小异,使用上也相同,区别只是在interface中声明了之后,在implementation中不能申明同名变量。

[cpp]  view plain copy
  1. NSLog(@"hello %@",atany);  


3)只在.h中使用@property声明一个变量。

声明:

[cpp]  view plain copy
  1. @property (nonatomic, retain) NSString *atany;  

 
调用 
:不在.m文件中用@synthesize合成变量的话,系统会调用Autosynthesize自动生成一个以下划线+变量名为名称的实例变量。 
 

调用方式除了可以用self.atany这种形式,_atany也可以。

[cpp]  view plain copy
  1. NSLog(@"hello %@",self.atany);  
  2. NSLog(@"hello %@",_atany);  

4).h中用@property;.m中使@synthesize+ 变量的形式合成变量。

声明

[cpp]  view plain copy
  1. @property (nonatomic, retain) NSString *atany;  
  2. @synthesize atany;  
 

 
调用 
:如果使用@synthesize合成,则不会自动生成实例变量_atany,而是atany。 
 

[cpp]  view plain copy
  1. NSLog(@"hello %@",self.atany);  
  2. NSLog(@"hello %@",atany);  
 

 

5).h中用@property;.m中使用@synthesize 变量 = _变量的形式。

声明

[cpp]  view plain copy
  1. @property (nonatomic, retain) NSString *atany;  
  2. @synthesize atany = _atany;  
 

 
调用 
 
使用 
@synthesize 变量 = _变量的话,真正的实例变量是_atany 
 

[cpp]  view plain copy
  1. NSLog(@"hello %@",self.atany);  
  2. NSLog(@"hello %@",_atany);  
 

 
 
 
 

对比4)与5)两种方式,是不是很奇怪怎么有时候是atany有时候是_atany?

其实简单来说@synthesize就是声明getter、setter方法。

那么如4)这种方式。@synthesize atany 对应着getter方法为:

[cpp]  view plain copy
  1. -(int)atany  
  2. {  
  3.    return  atany;  
  4. }  
 

 
而5)话@synthesize atany = _atany则是: 
 

[cpp]  view plain copy
  1. -(int)atany  
  2. {  
  3.    return  _atany;  
  4. }  
atany实际上是方法名,_atany才是实例变量,那么为什么oc不像java那样有getAtany的形式呢?

     原因是在Object-C里的accessor(存取方法)中,不会用getAtany这种形式,因为Get这个词在Cocoa中有着特殊的含义。如果get出现在方法名称中,则代表了这个方法传递的参数会作为指针类型处理。如果乱用Get的话,也会出现一些Bug。


三、常用声明方式与推荐声明方式

在网上你会经常见到这种

[cpp]  view plain copy
  1. @interface MethodOne : NSObject{  
  2.     NSString *atany;  
  3. }  
  4. @property (nonatomic, retain) NSString *atany;  

这不是重复声明吗?我们看看下面两种情况:

1)在.m中使用@synthesize atany(等同于不写@synthesize情况)

[cpp]  view plain copy
  1. atany = @"atany 1";  
  2. NSLog(@"hello %@",self.atany);  
  3. NSLog(@"hello %@",atany);  
我们知道self的形式是调用getter方法,atany是直接访问变量,那么赋值atany为atany 1,看输出:

2013-09-09 16:39:05.422TestVar[11376:c07] hello atany 1

2013-09-09 16:39:05.422 TestVar[11376:c07] hello atany 1

说明两者相同,也就是说@interface中的声明是多余的。


2)在.m中使用@synthesize atany = _atany。

[cpp]  view plain copy
  1. atany = @"atany 1";  
  2. _atany = @"atany 2";  
  3. NSLog(@"hello %@",self.atany);  
  4. NSLog(@"hello %@",_atany);  
  5. NSLog(@"hello %@",atany);  
输出为:

2013-09-09 16:36:28.356TestVar[11347:c07] hello atany 2

2013-09-09 16:36:28.356TestVar[11347:c07] hello atany 2

2013-09-09 16:36:28.356TestVar[11347:c07] hello atany 1

实际上atany与_atany已经是不同的两个实例变量了。

如上面描述而言:

建议是:

1.如果只是单纯的private变量,那么写在interface中与声明在implementation里都可以。

2.如果是public属性,就用property写在.h文件里,在.m文件中使用@synthesize atany = _atany; 比较好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值