Categories Add Methods to Existing Classes

< Programming with Object-C  page 68>

If you need to add a method to an existing class, perhaps to add functionality to make it easier to do something in your own application, the easiest way is to use a category.

The syntax to declare a category uses the @interface keyword, just like a standard Objective-C class description,but does not indicate any inheritance from a subclass. Instead, it specifies the name of the category inparentheses, like this: 

// 分类的声明:

#import "ClassName.h"

@interface ClassName (CategoryName)  

@end

// 分类的实现:

#import "ClassName+CategoryName"

@implementation ClassName (CategoryName)

@end

A category can be declared for any class, even if you don’t have the original implementation source code (such as for standard Cocoa or Cocoa Touch classes). Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class. At runtime, there’s no difference between a method added by a category and one that is implemented by the original class.

// 分类可以定义实例方法和类方法

// 不可定义实例变量,但是可以定义属性变量,定义的属性变量系统不会自动合成setter和getter方法

Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class. 

The only way to add a traditional property—backed by a new instance variable—to an existing class is to usea class extension 

// 为了避免冲突在分类方法前加上自定义的前缀

In order to avoid undefined behavior, it’s best practice to add a prefix to method names in categories onframework classes, just like you should add a prefix to the names of your own classes . You might choose touse the same three letters you use for your class prefixes, but lowercase to follow the usual convention formethod names, then an underscore, before the rest of the method name. For the NSSortDescriptor example,your own category might look like this: 

@interface NSSortDescriptor (XYZAdditions)
+ (id)xyz_sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending;
@end

This means you can be sure that your method will be used at runtime. The ambiguity is removed because yourcode now looks like this: 

NSSortDescriptor *descriptor =
[NSSortDescriptor xyz_sortDescriptorWithKey:@"name" ascending:YES]; 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值