新浪微博 有道云笔记 麦库 EverNote Pocket Instapaper 更多

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


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

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

那如何才能实现分工合作呢?下面做一下验证:例子来自《iOS5开发基础教程》第8章(也就是《iOS5开发基础教程》最新版的“08 - Sections2”下载地址:http://vdisk.weibo.com/s/hBHg6

对该实例就行如下修改:

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),也就是sum:方法;以及单独新建.h声明方法,将category方法分散到不同的.m文件(此实例中只用到一个.m文件)调用实现。

实例完整工程下载地址:Hi,推荐文件给你 "tableViewTest.zip" http://vdisk.weibo.com/s/if_0R

posted @ 2012-11-16 12:30  iTeaTime(技术清谈)  阅读(344)  评论(0编辑  收藏  举报