• category添加属性


    category添加属性

    面试题

    1. Category的实现原理,以及Category为什么只能加方法不能加属性。
    2. Category中有load方法吗?load方法是什么时候调用的?load 方法能继承吗?
    3. load、initialize的区别,以及它们在category重写的时候的调用的次序。

    官方文档

    You use categories to define additional methods of an existing class—even one whose source code is unavailable to you—without subclassing. You typically use a category to add methods to an existing class, such as one defined in the Cocoa frameworks. The added methods are inherited by subclasses and are indistinguishable at runtime from the original methods of the class. You can also use categories of your own classes to:
    1.Distribute the implementation of your own classes into separate source files—for example, you could group the methods of a large class into several categories and put each category in a different file.
    2.Declare private methods.
    You add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are an extension to a class declared elsewhere, not a new class.

    翻译:

    您可以使用类别定义现有类的附加方法,即使是没有子类的源代码不可用的方法。通常使用类别向现有类添加方法,例如在COCOA框架中定义的类。添加的方法由子类继承,并且在运行时与类的原始方法不可区分您还可以使用自己类的类别来:

    1. 将自己类的实现分发到单独的源文件中例如,可以将一个大型类的方法分组到多个类别中,并将每个类别放入不同的文件中。

    2. 声明私有方法。

    通过在接口文件中以类别名称声明方法并在实现文件中以相同名称定义方法,可以将方法添加到类中。类别名称表示这些方法是在别处声明的类的扩展,而不是新类。 更多内容请查看category的官方文档说明

    实现分类

    新建一个person类 添加属性name

        //Person.h
        @interface Person : NSObject
        @property (nonatomic , strong) NSString * name;
        @end
        //Person.m
        @implementation Person
    
        @end

    建立一个person + test 分类,声明一个age属性

        @interface Person (test)
        @property (nonatomic , assign) int  age;
        @end
    

    创建两个实例化对象来验证分类属性age是否可用

        Person *person1 = [[Person alloc]init];
        person1.name  = @"person1";
        person1.age = 10;
    
        Person *person2 = [[Person alloc]init];
        person2.name  = @"person2";
        person2.age = 60;
    
        NSLog(@"person1的name:%@ age:%d",person1.name,person1.age);
        NSLog(@"person2的name:%@ age:%d",person2.name,person2.age);

    做到这一步时候可以运行下看到-[Person setAge:]: unrecognized selector sent to instance 0x600000db88d0的错误提醒。说明我们person+test里的age并没有真正生成,或者说是和原类person里的name属性不一致。我们知道在原类声明name 其实就是系统帮我们生成了属性声明,setter和getter方法。所以我们在分类的属性声明里,自己添加分类属性的这三种方法来试试看能不能生效。

        #import "Person.h"
        @interface Person()
        {
            NSString *_name;
        }
        @end
        @implementation Person
        - (void)setName:(NSString *)name{
            _name = name;
        }
        - (NSString *)name{
            return _name;
        }
        @end
        #import "Person+test.h"
        static NSInteger age1 = 0;
        @implementation Person (test)
        - (void)setAge:(NSInteger)age{
            age1 = age;
        }
        - (NSInteger)age{
            return age1;
        }
        @end

    打印信息

        person1的name:person1 age:60
        person2的name:person2 age:60

    打印信息可以看到age的值只存了一个最后一个。无法保留原先的值,所以这种方式占用的同一块内存空间。 以上就是说明分类不能直接添加属性,接下来就讲讲分类添加属性的四种方式。

    分类添加属性的四种方式

    第一种:(不推荐,只是实现效果)

    通过字典来存储变量,每个值一对一存储在字典里,通过self对象来做为key 真正的值作为value 弊端:字典需要一直存在,每添加一个属性,字典都会不断的扩容,分类的字典是在load方法里创建。

    #import "Person+test.h"
    static NSMutableDictionary *dict;
    @implementation Person (test)
    +(void)load{
        dict = [NSMutableDictionary dictionary];
    }
    - (void)setAge:(NSInteger)age{
        [dict setObject:[NSString stringWithFormat:@"%ld",age] forKey:[NSString stringWithFormat:@"%@",self]];
    }
    - (NSInteger)age{
    
        return [[dict objectForKey:[NSString stringWithFormat:@"%@",self]] integerValue];
    }
    @end

    第二种:(常用)

    #import "Person+test.h"
    #import <objc/runtime.h>
    static NSString * agekey = @"agekey";
    @implementation Person (test)
    
    - (void)setAge:(NSInteger)age{
        objc_setAssociatedObject(self, &agekey, @(age), OBJC_ASSOCIATION_ASSIGN);
    }
    - (NSInteger)age{
    
        return [objc_getAssociatedObject(self, &agekey) integerValue];
    }
    
    @end

    第三种:(常用)

    #import "Person+test.h"
    #import <objc/runtime.h>
    @implementation Person (test)
    - (void)setAge:(NSInteger)age{
        objc_setAssociatedObject(self, &("age"), @(age), OBJC_ASSOCIATION_ASSIGN);
    }
    - (NSInteger)age{
    
        return [objc_getAssociatedObject(self, &("age")) integerValue];
    }
    @end

    第四种:(极力推荐)

    推荐原因:所有方法都有系统方法提示,不会出现setter/getter存储ID不一致问题。手打字符串容易出问题。

    #import "Person+test.h"
    #import <objc/runtime.h>
    @implementation Person (test)
    - (void)setAge:(NSInteger)age{
        objc_setAssociatedObject(self, @selector(age), @(age), OBJC_ASSOCIATION_ASSIGN);
    }
    - (NSInteger)age{
    
        return [objc_getAssociatedObject(self, _cmd) integerValue];
    }
    @end
  • 相关阅读:
    免费使用Adobe Flash Builder 4.6方法
    MySQL中concat函数
    PHP漏洞全解
    discuz 论坛 解决投票后不返回,提示AJAX错误的折中解决办法 common.js错误
    CoreSeek快速安装
    JSP基本语法
    『JAVA基础』使用JavaMail发邮件的实例 (转载)
    struts2过滤器和拦截器的区别
    【转】 myeclipse安装svn插件的多种方式
    用Java编写计算器的几种常见的做法
  • 原文地址:https://www.cnblogs.com/lovemargin/p/11936033.html
Copyright © 2020-2023  润新知