关于分类(category)和类的扩展(extensions)

《iOS5开发》

关于分类的验证:

分类的一大特性就是可以
:将类的实现分散到多个不同文件或多个不同框架中。分类允许分开编译,也就是说,同一个类也可以进行多人的分工合作;

那如何才能实现分工合作呢?下面做一下验证:例子来自《iOS5开发基础教程》08-TableView

Ctrl+N新建:

//  NSDictionary-MutableDeepCopy.h

#import <Foundation/Foundation.h>
@interface NSDictionary(MutableDeepCopy)
-(NSMutableDictionary *)mutableDeepCopy:(id)dic;
@end

//  BIDTableViewViewController.m
#import "BIDTableViewViewController.h"
#import "NSDictionary-MutableDeepCopy.h"
@interface BIDTableViewViewController ()
{
    NSInteger *abc;
}
-(NSInteger)sum:(NSInteger)number;
@end

@implementation BIDTableViewViewController
@synthesize search;
@synthesize table;
@synthesize staticDictionary;
@synthesize keyOfSearched;
@synthesize namesOfSearched;
//@synthesize namesOfSearched;


- (NSMutableDictionary *)mutableDeepCopy:(id)dic {
    NSMutableDictionary *returnDict = [NSMutableDictionary dictionaryWithCapacity:[dic count]];
    NSArray *keys = [dic allKeys];
    for (id key in keys) {
        id oneValue = [dic valueForKey:key];
        id oneCopy = nil;
        
        if ([oneValue respondsToSelector:@selector(mutableDeepCopy)])
            oneCopy = [oneValue mutableDeepCopy:oneValue];
        else if ([oneValue respondsToSelector:@selector(mutableCopy)])
            oneCopy = [oneValue mutableCopy];
        if (oneCopy == nil)
            oneCopy = [oneValue copy];
        [returnDict setValue:oneCopy forKey:key];
    }
    return returnDict;
}

-(NSInteger)sum:(NSInteger)number {
    return(number+5);
}




表视图运行正常,并且输出5,

中间的例子就体现了分类与扩展的思想,用到了两种分类的方法:.m文件声明与实现(extension),以及单独新建.h声明方法,将category方法分散到.m文件调用实现。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值