• 关于分类(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


    作者:
    出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
    本文版权归作者和博客园共有,欢迎转载,
    但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    软件工程的实践项目的自我目标
    transform使用导致元素内字体出现模糊的坑~~~
    nvmw安装,用于控制node版本;
    开章大吉~
    eclipse运行Android项目出现“The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. ”
    Date对象相关函数使用
    Balsamiq Mockups 注册码
    如何关闭sublime更新提示
    如何在边框中加入文字
    如何用手机测试移动端页面
  • 原文地址:https://www.cnblogs.com/ChenYilong/p/2808609.html
Copyright © 2020-2023  润新知