美文网首页
笔记04 category

笔记04 category

作者: PPFSaber | 来源:发表于2021-03-12 17:38 被阅读0次

    四 category



    越靠后编译的分类方法插入的越靠前,即优先级越高,编译顺序在 compile source 里


    分类的属性
    1 声明 分类的属性系统只会帮你声明 set和get 方法,不会实现这两个方法,需要你自己去实现
    @property (copy, nonatomic) NSString* name;

    关联对象

    另一种方式 
    #define MJKey [NSString stringWithFormat:@"%p", self]
    @implementation MJPerson (Test)
    
    NSMutableDictionary *names_;
    NSMutableDictionary *weights_;
    + (void)load
    {
        weights_ = [NSMutableDictionary dictionary];
        names_ = [NSMutableDictionary dictionary];
    }
    
    - (void)setName:(NSString *)name
    {
    //    NSString *key = [NSString stringWithFormat:@"%p", self];
        names_[MJKey] = name;
    }
    
    - (NSString *)name
    {
    //    NSString *key = [NSString stringWithFormat:@"%p", self];
        return names_[MJKey];
    }
    
    - (void)setWeight:(int)weight
    {
    //    NSString *key = [NSString stringWithFormat:@"%p", self];
        weights_[MJKey] = @(weight);
    }
    
    - (int)weight
    {
    //    NSString *key = [NSString stringWithFormat:@"%p", self];
        return [weights_[MJKey] intValue];
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:笔记04 category

          本文链接:https://www.haomeiwen.com/subject/tbwoqltx.html