!!!Obj-C 2.0 -- Chapter 5 Categories and Extensions
来源:互联网 发布:数据交换平台监控 编辑:程序博客网 时间:2024/05/01 12:16
A category allows you to add methods to an existing class -- even to one to which you don't have the source.
5.1 Adding Methods to Classes
You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class.
You cannot use a category to add additional instance variables to a class.
The methods the category adds to the class are inherited by all the class's subclasses, just like other methods.
The declaration of a category interface looks similar to a class declaration -- except the category name is listed within parentheses after the class name and the superclass isn't mentioned. Unless its methods don't access any instance variables of the class, the category must import the interface file for the class it extend:
#import "ClassName.h"@interface ClassName ( CategoryName )// method declarations@endFor Category files, a common naming convention is that the base file name of the category is the name of the class the category extends followed by "+" followed by the name of the category: (ClassName+CategoryName.m):
#import "ClassName+CategoryName.h"@implementation ClassNmae ( CategoryName )// method definitions@endAll instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.
5.2 How you Use Categories
Although the language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from using this functionality.
5.4 Extensions
Class extensions are like "anonymous" categories, except that the methods they declare must be implemented in the main @implementation block for the corresponding class.
It is common for a class to have a publicly declared API and to then have additional API declared privately for use solely by the class or the framework within which the class resides (E.G. readwrite/readonly property attributes).
// Category implementation, can compile successfully@interface MyObject : NSObject{ NSNumber *number;}- (NSNumber *) number;@end@interface MyObject (Setter) // category- (void)setNumber: (NSNumber *)newNumber;@end@implementation MyObject-( NSNumber *) number { return number;}@end
This is no implementation of the setNumber: method, if it's invoked, it will generate error.
// Category implementation, can compile successfully@interface MyObject : NSObject{ NSNumber *number;}- (NSNumber *) number;@end@interface MyObject () // extension- (void)setNumber: (NSNumber *)newNumber;@end@implementation MyObject-( NSNumber *) number { return number;}-(void)setNumber:(NSNumber *)newNumber{ number = newNumber;}@end
This is no implementation of the setNumber: method, if it's invoked, it will generate error.
Note:
1. No name is given in theparentheses in the second @interface block.
2. The implementation of thesetNumber: method appears within the main @implementation block for the class.
For one class, we canhave multiple categories, but we can only have one extension.
Extension can addinstance variable and override property!
- !!!Obj-C 2.0 -- Chapter 5 Categories and Extensions
- !!!Obj-C 2.0 -- Chapter 1 Objects, Classes, and Messaging
- !!!Obj-C 2.0 -- Chapter 3 Allocating and Initializing Objects
- !!!Obj-C 2.0 -- Chapter 4 Declared Properties
- !!!Obj-C 2.0 -- Chapter 6 Protocols
- !!!Obj-c on Mac --- Chapter 5 ~ 7
- !!!Obj-c on Mac --- Chapter 15 File Loading and Saving
- Categories和Extensions
- !!!Obj-C 2.0 -- Chapter 2 Defining a Class
- iphone 开发Categories 、Extensions 区别
- iphone 开发Categories 、Extensions 区别
- iphone 开发Categories 、Extensions 区别
- iphone 开发Categories 、Extensions 区别
- !!!Obj-c on Mac --- Chapter 2 ~ 4
- !!!Obj-c on Mac --- Chapter 13 Protocols
- !!!Obj-c on Mac --- Chapter 17 NSPredicate
- iOS中Categories和Extensions的区别
- iOS - Categories&&Extensions (类别和扩展)
- 测试JSTL_EL表达式
- 90后:象子弹飞,象蛋壳脆
- android onNewIntent
- 简单批处理重定向JBOSS控制台输出
- 梦想改变世界
- !!!Obj-C 2.0 -- Chapter 5 Categories and Extensions
- Sublime Text2 一些好用的Package
- 基于FBReader的andriod电子书模板
- call_user_func_array()的用法
- 如果判断数组是一维还是二维?
- Counting Unique k-mers -- My First Go Program
- PIC18f46j50单片机里SPI部分的一点程序
- Swap Nodes In Pairs
- 2013年重新回到csdn 但这次我要重新学习java