iOS声明变量详解

来源:互联网 发布:matlab中矩阵的转置 编辑:程序博客网 时间:2024/05/18 22:40

内容概述:

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


atany原创,转载请注明博主与博文链接,3Q吐舌头,未经博主允许,不得进行商业用途

http://blog.csdn.net/yang8456211/article/details/11490119

—— by atany



一、@interface @property的区别

前言:

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

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


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

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

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


二、多种变量声明方式

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

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

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

@interface MethodOne : NSObject{    NSString *atany;}
调用:在.m文件中直接使用变量名调用即可。

NSLog(@"hello %@",atany);


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

声明

@implementation MethodOne{    NSString *atany;}

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

NSLog(@"hello %@",atany);


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

声明:

@property (nonatomic, retain) NSString *atany;

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

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

NSLog(@"hello %@",self.atany);NSLog(@"hello %@",_atany);


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

声明

@property (nonatomic, retain) NSString *atany;@synthesize atany;

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

NSLog(@"hello %@",self.atany);NSLog(@"hello %@",atany);

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

声明

@property (nonatomic, retain) NSString *atany;@synthesize atany = _atany;

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

NSLog(@"hello %@",self.atany);NSLog(@"hello %@",_atany);

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

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

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

 -(int)atany {    return  atany; }

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

 -(int)atany {    return  _atany; }
atany实际上是方法名,_atany才是实例变量,那么为什么oc不像java那样有getAtany的形式呢?

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


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

在网上你会经常见到这种

@interface MethodOne : NSObject{    NSString *atany;}@property (nonatomic, retain) NSString *atany;

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

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

atany = @"atany 1";NSLog(@"hello %@",self.atany);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。

atany = @"atany 1";_atany = @"atany 2";NSLog(@"hello %@",self.atany);NSLog(@"hello %@",_atany);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; 比较好。



	
				
		
原创粉丝点击