iOS声明变量详解【转】
来源:互联网 发布:二手知豆电动汽车价格 编辑:程序博客网 时间:2024/05/17 20:09
内容概述:
本文主要讲述了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。
- @interface MethodOne : NSObject{
- NSString *atany;
- }
- 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;
调用方式除了可以用self.atany这种形式,_atany也可以。
- NSLog(@"hello %@",self.atany);
- NSLog(@"hello %@",_atany);
4)在.h中用@property;在.m中使用@synthesize+ 变量的形式合成变量。
声明:
- @property (nonatomic, retain) NSString *atany;
- @synthesize atany;
- NSLog(@"hello %@",self.atany);
- NSLog(@"hello %@",atany);
5)在.h中用@property;在.m中使用@synthesize 变量 = _变量的形式。
声明:
- @property (nonatomic, retain) NSString *atany;
- @synthesize atany = _atany;
- NSLog(@"hello %@",self.atany);
- NSLog(@"hello %@",_atany);
注:对比4)与5)两种方式,是不是很奇怪怎么有时候是atany有时候是_atany?
其实简单来说@synthesize就是声明getter、setter方法。那么如4)这种方式。@synthesize atany 对应着getter方法为:
- -(int)atany
- {
- return atany;
- }
- -(int)atany
- {
- return _atany;
- }
原因是在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);
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中的声明是多余的。
- 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声明变量详解【转】
- iOS声明变量详解
- iOS声明变量详解
- extern声明变量详解
- ios 变量声明(一)
- Swift详解之一 -------- 变量声明
- 详解js变量声明提升
- IOS关于变量声明的例子
- IOS中两种变量声明方法的区别
- C++中的extern声明变量详解
- 变量声明和函数声明的意义详解
- 【iOS开发】 类变量(实例变量)声明的含义
- 【转】JS中的变量声明
- 变量声明
- 变量声明
- 声明变量
- 变量声明
- 变量声明
- 国产手机出海:提着互联网灯笼去印度
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
- PullScrollView进阶(一)----->图片下拉回弹
- 苹果审核reject理由大全(7):引导更新按钮或弹框
- Paratroopers
- iOS声明变量详解【转】
- 零基础学python-2.13 回到我们的游戏 加入循环
- jQuery(2)选择器
- java做图片加水印的功能代码
- 印度将成智能手机市场下一个风口,群雄已开始逐鹿
- 成都麻将胡牌规则
- Android事件处理的两种模型介绍
- 最新版 使用Xcode6.4 和Instruments、Leaks调试解决iOS内存泄露
- jQuery(3)动态创建元素和常用函数示例