iOS声明变量详解【转】
2015-08-12 12:24
489 查看
内容概述:
本文主要讲述了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
plaincopy
@interface MethodOne : NSObject{
NSString *atany;
}
调用:在.m文件中直接使用变量名调用即可。
[cpp] view
plaincopy
NSLog(@"hello %@",atany);
2)不在.h中声明变量,而直接在.m的@implementation中声明变量。
声明:
[cpp] view
plaincopy
@implementation MethodOne{
NSString *atany;
}
调用:这种声明方式与1)大同小异,使用上也相同,区别只是在interface中声明了之后,在implementation中不能申明同名变量。
[cpp] view
plaincopy
NSLog(@"hello %@",atany);
3)只在.h中使用@property声明一个变量。
声明:
[cpp] view
plaincopy
@property (nonatomic, retain) NSString *atany;
调用:不在.m文件中用@synthesize合成变量的话,系统会调用Autosynthesize自动生成一个以下划线+变量名为名称的实例变量。
调用方式除了可以用self.atany这种形式,_atany也可以。
[cpp] view
plaincopy
NSLog(@"hello %@",self.atany);
NSLog(@"hello %@",_atany);
4)在.h中用@property;在.m中使用@synthesize+
变量的形式合成变量。
声明:
[cpp] view
plaincopy
@property (nonatomic, retain) NSString *atany;
@synthesize atany;
调用:如果使用@synthesize合成,则不会自动生成实例变量_atany,而是atany。
[cpp] view
plaincopy
NSLog(@"hello %@",self.atany);
NSLog(@"hello %@",atany);
5)在.h中用@property;在.m中使用@synthesize
变量 = _变量的形式。
声明:
[cpp] view
plaincopy
@property (nonatomic, retain) NSString *atany;
@synthesize atany = _atany;
调用:使用@synthesize
变量 = _变量的话,真正的实例变量是_atany
[cpp] view
plaincopy
NSLog(@"hello %@",self.atany);
NSLog(@"hello %@",_atany);
注:对比4)与5)两种方式,是不是很奇怪怎么有时候是atany有时候是_atany?
其实简单来说@synthesize就是声明getter、setter方法。
那么如4)这种方式。@synthesize atany 对应着getter方法为:
[cpp] view
plaincopy
-(int)atany
{
return atany;
}
而5)话@synthesize atany = _atany则是:
[cpp] view
plaincopy
-(int)atany
{
return _atany;
}
atany实际上是方法名,_atany才是实例变量,那么为什么oc不像java那样有getAtany的形式呢?
原因是在Object-C里的accessor(存取方法)中,不会用getAtany这种形式,因为Get这个词在Cocoa中有着特殊的含义。如果get出现在方法名称中,则代表了这个方法传递的参数会作为指针类型处理。如果乱用Get的话,也会出现一些Bug。
三、常用声明方式与推荐声明方式
在网上你会经常见到这种
[cpp] view
plaincopy
@interface MethodOne : NSObject{
NSString *atany;
}
@property (nonatomic, retain) NSString *atany;
这不是重复声明吗?我们看看下面两种情况:
1)在.m中使用@synthesize atany(等同于不写@synthesize情况)
[cpp] view
plaincopy
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。
[cpp] view
plaincopy
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; 比较好。
本文主要讲述了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
plaincopy
@interface MethodOne : NSObject{
NSString *atany;
}
调用:在.m文件中直接使用变量名调用即可。
[cpp] view
plaincopy
NSLog(@"hello %@",atany);
2)不在.h中声明变量,而直接在.m的@implementation中声明变量。
声明:
[cpp] view
plaincopy
@implementation MethodOne{
NSString *atany;
}
调用:这种声明方式与1)大同小异,使用上也相同,区别只是在interface中声明了之后,在implementation中不能申明同名变量。
[cpp] view
plaincopy
NSLog(@"hello %@",atany);
3)只在.h中使用@property声明一个变量。
声明:
[cpp] view
plaincopy
@property (nonatomic, retain) NSString *atany;
调用:不在.m文件中用@synthesize合成变量的话,系统会调用Autosynthesize自动生成一个以下划线+变量名为名称的实例变量。
调用方式除了可以用self.atany这种形式,_atany也可以。
[cpp] view
plaincopy
NSLog(@"hello %@",self.atany);
NSLog(@"hello %@",_atany);
4)在.h中用@property;在.m中使用@synthesize+
变量的形式合成变量。
声明:
[cpp] view
plaincopy
@property (nonatomic, retain) NSString *atany;
@synthesize atany;
调用:如果使用@synthesize合成,则不会自动生成实例变量_atany,而是atany。
[cpp] view
plaincopy
NSLog(@"hello %@",self.atany);
NSLog(@"hello %@",atany);
5)在.h中用@property;在.m中使用@synthesize
变量 = _变量的形式。
声明:
[cpp] view
plaincopy
@property (nonatomic, retain) NSString *atany;
@synthesize atany = _atany;
调用:使用@synthesize
变量 = _变量的话,真正的实例变量是_atany
[cpp] view
plaincopy
NSLog(@"hello %@",self.atany);
NSLog(@"hello %@",_atany);
注:对比4)与5)两种方式,是不是很奇怪怎么有时候是atany有时候是_atany?
其实简单来说@synthesize就是声明getter、setter方法。
那么如4)这种方式。@synthesize atany 对应着getter方法为:
[cpp] view
plaincopy
-(int)atany
{
return atany;
}
而5)话@synthesize atany = _atany则是:
[cpp] view
plaincopy
-(int)atany
{
return _atany;
}
atany实际上是方法名,_atany才是实例变量,那么为什么oc不像java那样有getAtany的形式呢?
原因是在Object-C里的accessor(存取方法)中,不会用getAtany这种形式,因为Get这个词在Cocoa中有着特殊的含义。如果get出现在方法名称中,则代表了这个方法传递的参数会作为指针类型处理。如果乱用Get的话,也会出现一些Bug。
三、常用声明方式与推荐声明方式
在网上你会经常见到这种
[cpp] view
plaincopy
@interface MethodOne : NSObject{
NSString *atany;
}
@property (nonatomic, retain) NSString *atany;
这不是重复声明吗?我们看看下面两种情况:
1)在.m中使用@synthesize atany(等同于不写@synthesize情况)
[cpp] view
plaincopy
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。
[cpp] view
plaincopy
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; 比较好。
相关文章推荐
- ios scrollview正在滚动时 停止滚动 代码实现
- ios--网页js调用oc代码+传递参数+避免中文参数乱码的解决方案(实例)
- iOS 应用内付费(IAP)开发步骤四:客户端及服务端开发
- iOS 应用内付费(IAP)开发步骤三:申请测试账号
- ios读取系统相册以及获取图片的名字
- iOS调试 - 在系统抛出异常处设置断点
- IOS按钮排列自动换行
- iOS 创建单例的两种方法
- iOS 8系统自带毛玻璃效果实现
- nagios
- iOS 应用内付费(IAP)开发步骤一:填写相关的税务,银行,联系人信息;
- iOS中的分类(category)
- GitHub 上都有值得关注学习的 iOS 开源项目
- iOS开发 使用GCD实现多任务
- iOS中的NSArraySort(^块语法)
- iOS开发所有KeyboardType与图片对应展示
- iOS开发所有KeyboardType与图片对应展示
- iOS中的NSString
- iOS中的NSdate
- iOS新手必看之全貌