objective-c 中 category 和 extension 的区别
来源:互联网 发布:矩阵秩的性质 编辑:程序博客网 时间:2024/05/17 09:22
感谢原作者:http://www.cnblogs.com/CoderPlace/archive/2012/05/04/2483039.html
apple官方文档说extension是 匿名category,从形式上extension确实是不具名的category,但事实上差别很大。category就不细说,主要是期待subclass,为现有类动态添加新的方法。而引入extension的目的主要是Publicly-Readable, Privately-Writeable Properties,即实现外部只读,内部可写。如下所示,
// .h
@interface
MyClass :
NSObject
@property
(
readonly
, retain)
NSString
* myString;
@end
// .m
@interface
MyClass ()
@property
(
readwrite
, retain)
NSString
* myString;
@end
因此,extension 和 category 的不同主要有,
1. 形式上来看extension是匿名的category。
2. extension里边声明的方法必须在main implementation实现,类似于protocol里的@required。而category则声明的方法没有强制实现的要求。
3. extension 和 category 都不能定义新的实例变量。但extension可以定义新的property,然后在main implementation里@synthesize 新的property。而category定义新的property却不能@synthesize,这时可以使用Associative References技术实现对新定义的property的getter和setter。
0 0
- objective-c 中 category 和 extension 的区别
- Objective-C Category和Extension
- Object-C Category和Extension的区别
- Object-C Category和Extension的区别
- iOS中 category 和extension的区别
- iOS中 category 和extension的区别
- Objective-C基础1-类目(Category)和延展(Extension)的区别
- Objective-C中Extension与Category的使用
- Objective-C中Extension与Category的使用
- Objective-C中Extension与Category的使用
- Objective-c的category(分类)和Extension(扩张)
- Objective-C分类 (category)和扩展(Extension)
- objective-c之Category和Extension
- Objective-C分类 (category)和扩展(Extension)
- 【Objective-C】分类 (category)和扩展(Extension)
- Objective-C分类 (category)和扩展(Extension)
- Objective-c 中的Category和Extension
- Objective-C分类 (category)和扩展(Extension)
- ReactNative学习十一-手写Item布局
- ARM平台嵌入式Linux下SPI设备连接
- unity shader入门
- 数据库事务(三)
- 分布式开发 | 服务器划分简介
- objective-c 中 category 和 extension 的区别
- 求Maximim Gap 排序后的两个相邻元素之间的最大差值
- 开启ssh服务
- 详解window.history
- Shell排序
- android中File文件的读的实现
- Distinctive Image Features from Scale-Invariant Keypoints(SIFT)
- python 异常 try-except句型
- Linux下的多路复用和unix套接字的综合使用