ios 中Category类别(扩展类)小结

类别

类别是一种为现有的类添加新方法的方式。
利用Objective-C的动态运行时(runtime)分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为任何类添加新的方法,包括那些没有源代码的类。
类别使得无需创建对象类的子类就能完成同样的工作。
一、创建类别

1、声明类别
声明类别与声明类的形式很相似:

@interface  NSString(NumberConvenience)
-(NSNumber *)lengthAsNumber;
@end//NumberConvenience

这个声明有两个特点:
     (1)现有的类位于@interface关键字之后,其后是位于圆括号中的类别名称。类别名称是NumberConvenience,而且该类别将向NSString类中添加方法。换句话说:“我们向NSString类中添加一个名称为NumberConvenience的类别。”
        同名类别有唯一性,但是可以添加任意多的不同名类别。
     (2)可以执行希望向其添加类别的类以及类别的名称,还可以列出添加的方法
        不可以添加新的实例变量,类别生命中没有实例变量部分。


2、实现类别

    @implementation NSString(NumberConvenience)
    -(NSNumber *)lengthAsNumber
    {
        unsigned int length = [self length];
        return ([NSNumber numberWithUnsignedInt : length]);
    }  //lengthAsNumber
    @end   //NumberConvenience

在实现部分也包括类名、类别名和新方法的实现代码。

3、类别的局限性
有两方面局限性:
    (1)无法向类中添加新的实例变量,类别没有位置容纳实例变量。
    (2)名称冲突,即当类别中的方法与原始类方法名称冲突时,类别具有更高的优先级。类别方法将完全取代初始方法从而无法再使用初始方法。
注意:无法添加实例变量的局限可以使用字典对象解决。
4、类别的作用
类别主要有3个作用:
    (1)将类的实现分散到多个不同文件或多个不同框架中。
    (2)创建对私有方法的前向引用。
    (3)向对象添加非正式协议。


二、利用类别分散实现
我们可以将类的接口放入头文件中,从而将类的实现放入.m文件中
但不可以将@implementation分散到多个不同的.m文件中,使用类别可以完成这一工作,
利用类别,可以将一个类的方法组织到不同的逻辑分组中,使编程人员更加容易的阅读头文件
举例代码:
头文件CatagoryThing.h包含类的声明和一些类别,导入Foundation框架,然后带有3个整型变量的声明:

#import<Foundation/Foundation.h>
@interface CategoryThing : NSObject {
   int thing1;
   int thing2;
   int thing3;
}
@end // CategoryThing

类声明之后是3个类别,每个类别具有一个实例变量的访问器,将这些实现分散到不同的文件中:

@interface CategoryThing(Thing1)
- (void) setThing1: (int) thing1;
- (int) thing1;
@end // CategoryThing (Thing1)

@interface CategoryThing (Thing2)
- (void) setThing2: (int) thing2;
- (int) thing2;
@end // CategoryThing (Thing2)

@interface CategoryThing (Thing3)
- (void) setThing3: (int) thing3;
- (int) thing3;
@end // CategoryThing (Thing3)


注意:1.类别可以访问其继承的类的实例变量;

         2.类别的方法具有最高的优先级;

         3.类别可以分散到不同文件中,甚至不同框架中。

三、使用类别创建前向引用
如果其他类中的方法未实现,在你访问其他类的私有方法时编译器报错,
这时使用类别,在类别中声明这些方法(不必提供方法实现),编译器就不会再产生警告。

四、非正式协议和委托类别
Cocoa中的类经常使用一种名为委托(delegate)的技术
委托是一种对象,另一个类的对象会要求委托对象执行他的某些操作:

#import <Foundation/Foundation.h>
#import "ITunesFinder.h"
int main (int argc, c*****t char *argv[])
{
  NSAutoreleasePool *pool;
  pool = [[NSAutoreleasePool alloc] init];
  NSNetServiceBrowser*browser;
  browser = [[NSNetServiceBrowseralloc] init];
  ITunesFinder *finder;
     //因为finder是alloc方法创建的,必须在不适用这个对象时将其释放
  finder = [[ITunesFinder alloc] init];
 
     //告知browser使用finder作为委托对象
  [browser setDelegate:finder];
 
  //告知browser对象使用TCP协议去搜索DAAP类型服务
  [browser searchForServicesOfType: @"_daap._tcp" 
  inDomain: @"local."];//表示只搜索本地
 
  //表示下面的run循环已经开始
  NSLog (@"begin browsing");
 
  //run循环是一种Cocoa构造,他不执行任何处理,等待用户的操作,run方法将一直保持运行而不返回,所以包含此行之后的代码不会被运行
  [[NSRunLoop currentRunLoop] run];
 
  [browser release];
  [finder release];
  [pool release];
  return (0);

} // main

创建一个NSObject的类别称为“创建一个非正式协议”,因为可以作为任何类的委托对象使用

响应选择器
选择器只是一个方法名称,但它以Objective-C运行时使用特殊方式编码,以快速执行查询
可以使用@selector()预编译指定选择器,其中方法名位于圆括号中。

例如之前的Car类的setEngine:方法的选择器是:@selector(setEngine:)
而Car类的setTire:atIndex;方法的选择器如下所示:@selector(setTire:atIndex;)

NSObject提供了一个名为respondsToSelector方法,该方法询问对象以确定其是否能够响应某个特定的消息
举例代码:
  Car *car = [[Car alloc] init];
  if([carrespondsToSelector:@selector(setEngine:)]){
    NSLog(@"hihi");
  }
选择器的其他应用
选择器可以被传递,可以作为方法的参数使用,甚至可以作为实例变量存储

小结
类别提供了向现有类添加新方法的手段,即使没有这些类的源代码
类别可以将对象的实现分散到多个不同的源文件、甚至多个不同的框架中
使用类别可以声明非正式协议,非正式协议是NSObject的一个类别,他可以列出对象能够响应的方法
非正式协议用于委托,委托是一种允许轻松定制对象行为的技术

类别是一种为现有的类添加新方法的方式。

 @interface NSString (NumberConvenience)
  -(NSNumber *)lengthAsNumber;
 @end

(1)为NSString类添加一个名称为NumberConveniencede的类别;类别名称具有唯一性,你可以向一个类中添加任意多的类别。
(2)可以指定希望向其添加类别的类(NSString),以及类别的名称(NumberConvenience),而且你还可以列出添加的方法,最后以@end结束;类别声明部分不能有实例变量部分
实现类别
@implementation NSString (NumberConvenience)
- (NSNmuber *)lengthAsNumber{
       unsigned int length = [self length];//获得字符串长度
       return ([NSNumber numberWithUnsignedInt :length]);

@end
#import <Foundation/Foundation.h>
#import "CategoryThing.h"

//类别的作用:
//(1)将类别实现分散到多个不同文件或多个不同框架中
//(2)创建私有方法的前向引用
//(3)向对象添加非正式协议
//类别的局限性:
//(1)无法添加新的实例变量
//(2)名称冲突,如果类别和现有的方法重名,类别具有更高的优先级,解决办法,类别方法名中添加一个前缀
@interface NSString (NumberConvenience)
- (NSNumber *) lengthAsNumber;
@end


@implementation NSString (NumberConvenience)
- (NSNumber *) lengthAsNumber
{
  unsigned int length= [self length];
  return ([NSNumber numberWithUnsignedInt:length]);
}
@end

int main (int argc, c*****t char * argv[]) {
  //我们适用类别创建的所有NSNumber类的对象将在自动释放池中被销毁一样,可变字典也将在这里被销毁
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  // insert code here...

  NSMutableDictionary *dict;
  dict=[NSMutableDictionary dictionary];

  //使用键@“hello”将整值5添加到字典中的代码如下
  [dict setObject:[@"hello" lengthAsNumber] forKey: @"hello"];

  [dict setObject:[@"iLikeFish" lengthAsNumber] forKey: @"iLikeFish"];

  [dict setObject:[@"Once upon a time" lengthAsNumber] forKey: @"Once upon a time"];

    NSLog(@"%@",dict);

  CategoryThing *thing;
  thing= [[CategoryThing alloc] init];

  [thing setThing1:5];
  [thing setThing2:23];
  [thing setThing3:42];

  NSLog(@"Thing are %@!",thing);

  [thing release];

     [pool drain];
     return 0;
}


#import <Cocoa/Cocoa.h>
@interface CategoryThing : NSObject {
  int thing1;
  int thing2;
  int thing3;
}
@end

@interface CategoryThing (Thing1)
  - (void) setThing1:(int) thing1;
  - (int) thing1;
@end

@interface CategoryThing (Thing2)
- (void) setThing2:(int) thing2;
- (int) thing2;
@end

@interface CategoryThing (Thing3)
- (void) setThing3:(int) thing3;
- (int) thing3;
@end

#import "CategoryThing.h"
@implementation CategoryThing
- (NSString *) description
{
  NSString *desc;
  desc= [NSString stringWithFormat:@"%d %d %d",
     thing1,thing2,thing3];

  return (desc);
}
@end

#import "CategoryThing.h"
@implementation CategoryThing (Thing1)
- (void) setThing1:(int)t1
{
  thing1=t1;
}
- (int) thing1
{
  return (thing1);
}
@end

#import "CategoryThing.h"
@implementation CategoryThing (Thing2)
- (void) setThing2:(int)t2
{
  thing2=t2;
}
- (int) thing2
{
  return (thing2);
}

#import"CategoryThing.h"
@implementation CategoryThing (Thing3)
- (void) setThing3:(int)t3
{
  thing3=t3;
}
- (int) thing3
{
  return(thing3);
}
@end

原文地址:https://www.cnblogs.com/akiha/p/5743084.html