Objective-C:Categories

今天由于要看ObjC的代码,学点基本知识,水平很有限!

published by john at 5:21 am under iphone, Objective-C

As an alternative to subclassing, Objective-C categories provide a means to add method to a class.

Objective-C categories提供一个可选的把方法加入到类的方式对于子集。

What's intriguing, is that any methods that you add through a category become part of the class definition, so to speak.

引起兴趣的是,通过category你加入的方法会成为类定义中的一部分,就这样说吧。

In other words, if you add a method to the NSString class, any instance, or subclass, of NSString will have access to that method.

换句话说,如果你加个方法到类NSString,NSString的任何实例或者子类肯定有访问这个方法的权利。

Defining a category is identical to defining the interface for a class, with one small exception: you add a category name inside a set of parenthesis after the interface declaration.

定义category是与为类定义interface相同的,一点变化的是:在interface定义后接上一对插入了category名字的圆括号。

the format is shown below:

@interface ClassToAddMethodsTo (category)
...methods 
@end


For example, below I've defined a category that adds a method to the NSString class.

The method reverseString adds the capability to all NSString objects to reverse the characters in the string.

方法reverseString对所有NSString的对象增加字符产反向字符的能力。

@interface NSString (reverse)
-(NSString)reverseString;
@end

As with the @interface declaration, the @implementation section changes only in that the category names is added to the definition.

与@interface段的申明一样,@implementation段的改变仅仅是category的名字加入到定义里去了。

Below is the implementation of the interface defined above. Notice how in both cases I added (reverse), which is the category name I assigned.

@implenmentation NSString (reverse)
-(NSString *) reverseString
{
    NSMutableString *reversedStr;
    int len = [self length];
 
    // auto released string
    reversedStr = [NSMutableString stringWithCapacity:len];

    // probably woefully ineffioient...
    while (len > 0)
    {
          [reversedStr appendString:[NSString stringWithFormat:@"%c", [self characterAtIndex:--len]]];
     }
     return reversedStr;
}
@end


What follows is a short example to showing how to one might use the above category.

接下来的例子就是展示怎么用上面的category的,大家要注意了。


 

#import <Foundation/Foundation.h>
#import <NSString+Reverse.h>
int main(int argc, const char *argv[])
{
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     NSString *str = [NSString stringWithString:@"Fubar"];
     
     NLog(@"string: %@", str");
     rev = [str reverseString];
     NSLog(@"reversed: %@", rev);
     [pool drain];
     return 0;
}

The output of the above example is shown here:

结果大家去试下就知道,我就不打了。

Overriding Methods

Categories can also be used to override methods the class inherits, again, providing an alternaive to subclassing.

categories常常用来为子类在类的继承上重写方法提供又一种可选的方法。

You can always access the overridden method using super.

你总用关键字super去访问重写的方法。

I would assume from an internal (compiler/code generation) persperctive, this could lead to less code/overhead as compared to creating a subclass solely to override a method (caveat: I have to no proof that is true).

Dividing Source Code into Separate File

Although I haven't given this a go, categories provide an interesting opportunity to disperse the implementation of  a class across one or more files.

The Objective-C Programming Guid lists several benefits of the including:

  • Opportunity to group together methods that perform similar tasks
  • Confiuring classes differently for various applications, yet maintaining one set of code.
  • 为各种应用程序配置不同的类,而且还能使这一代码集维持联系。

Naming Conventions 名称转换,要注意哦!

The recommended naming convention fo ra category is "ClassToAddMethodsTo+CatgoryName" for instance, I used the following filenames for the interface and implementation of the above category code:

  • NSString+Reverse.h
  • NSString+Reverse.m
Caveats

好吧!以下都是注意事项了。

You cannot add instance variables to a class throught a category. Also, category names must be unique across an application.

其实也没有什么,就是你不能在category加入变量,还有就是应用范围内你的名字保持唯一。OK

 

Source Code

I've attached the Xcode project for the above example if you'd like to give it a try.

In the next post I'll shwo another example of using categories - the example will show one approach for hiding methods within in a class,

as Objective-C does not offer support for private methods. 你可以用category来隐藏方法,做到private,ObjC是没有提供private的哦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值