我们可能都不太分的清.h文件和.m文件里各种结构的用途和区别,那我们来仔细的区分一下。
首先写一个class类:
.h文件:
@interface ClassName{ NSString* _value; } @property(nonatomic,assign)NSString* value; -(void)func;
.m文件:
@interface ClassName(){ } @end @synthesize value; @implementation ClassName -(void)func{ } @end
大体上就是这个格式。很多人,包括我,在创建和使用Class时,直接就使用这样的模板。这个模板里有一些有意思的小东西,值得探讨,比如:
1. 为什么.h文件和.m文件里各有1个@interface
?它们分别有什么用?
2. .h中,value为什么要定义2遍?
3. @synthesize
有什么用?
还有一些其它的问题,今天先解决上面提到的这几个。
为什么.h文件和.m文件里各有1个@interface?它们分别有什么用?
.h里面的@interface
,不消说,是典型的头文件,它是供其它Class调用的。它的@property
和functions,都能够被其它Class“看到”。
而.m里面的@interface
,在OC里叫作Class Extension,是.h文件中@interface
的补充。但是.m文件里的@interface
,对外是不开放的,只在.m文件里可见。
因此,我们将对外开放的方法、变量放到.h文件中,而将不想要对外开放的变量放到.m文件中(.m文件的方法可以不声明,直接用)。
有的同学看到Class Extension,可能会想到OC里的@protocol
。是的,它们都是对一个Class的扩展。不过它们的区别也很明显:
Class Extension只能用在能得到源代码的情况下,而@protocol
在得不到源码的时候也可以使用。
因此@protocol
一般用作对一些系统Class的扩展,常见的比如对NSString、UIView等。
.h中,value1为什么要定义2遍?
当然,现在@interface{}
里的定义也可以省略掉了,不过原理还是要搞清楚。
严格来说@interface{}
里定义的变量,叫作instance variable,它是这个Class内部真正的全局变量。然而这个instance variable是不对外公开的,因此我们还需要一个对外公开的东西来调用,就是@property
@property是对外的,它其实是告诉大家,我这个Class里,有一个变量的set/get方法。比如,@property NSString* string;
就是说,本Class里有一个getString/setString供你们调用。
因此需要2次声明。当然现在lldb也升级了,只要你声明了@property
,它就可以自动创建对应的全局变量。
@synthesize有什么用?
@property
一个变量后,在@implementation
里再@synthesize
一下,相信是很多人的习惯。但是为什么要有这个@synthesize方法呢?
@property
是对外声明了Class的get/set方法,然后我们就需要在.m文件里手写get/set方法。这可就麻烦了,1个变量对应2个方法,假如一个Class里有10个变量,那岂不是要写20个方法?烦也烦死唠。
@synthesize帮我们解决了这个问题。@synthesize
在.m文件里自动生成了get/set方法。因此,我们只要在@implementation
后面加上一行:@synthesize
就可以自动生成get/set方法了,省掉了很多麻烦。比如@synthesize value = _value;
的意思就是,将instance variable _value用作getValue和setValue方法里。
相关推荐
资源分类:Python库 所属语言:Python 资源全名:synthesize-0.0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
@interface XmlResolve : NSObject { } @property (nonatomic, retain)NSString *objName; @property BOOL *isList; @property (nonatomic, retain) NSMutableString *currentResult; @property (nonatomic,...
相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459
Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
Using a hands-on approach, you’ll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
[map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@...
Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
Objective-C、iOS开发、Mac OS X、编程语言、面向对象编程、内存管理、自动引用计数(ARC)、协议(protocol)、类扩展(category)、键值观察(KVO)、键值编码(KVC)、Block、Delegate模式、多态性、Singleton...
objective-c小技巧 1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat...
Purpose of this tutorial is to help you compose and implement a custom, OpenRISC ...C. adjust RAM module D. synthesize and place & route OpenRISC based system E. download F. test the OpenRISC system
The Xilinx® Vivado® High-Level Synthesis (HLS) tool transforms a C specification into a register transfer level (RTL) implementation that you can synthesize into a Xilinx field programmable gate ...
C. adjust RAM module D. synthesize and place & route OpenRISC based system E. download F. test the OpenRISC system The OpenRISC Platform source code is available through a CVS server. To make ...
3D-Synthesize3DviaDepthOrSil.zip,[CVPR 2017]通过建模多视图深度图或轮廓生成和重建三维形状,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他...
本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等
重新实现CRAFT-字符区域感知以进行文本检测客观的 复制论文提到的弱监督培训 ... 从预先训练的模型列表中获取预先训练的模型(当前仅可使用SYNTH-Text进行严格监督) 运行命令- python main.py synthesize --model=./mo
With VideoLab you can do just about anything imaginable: synthesize, capture, process, and analyze; mix, listen, visualize and more. Applications include: Video Capture - Record videos from ...
重新实现CRAFT-字符区域感知以进行文本检测 客观的 复制论文提到的弱监督培训 在所有流行的数据集上生成字符bbox。 使用命令行界面公开经过预训练的模型,以在自定义... python main.py synthesize --model=./model/
Synthesize Synthesize is an android library which can create layout images in background threads, services, etc without inflating them in activity or fragments. Synthesize Documentation
Drag and Drop Component Suite Version 4.1 Field test 5, released 16-dec-2001 ?1997-2001 Angus Johnson & Anders Melander ... ------------------------------------------- Table of Contents: ...