Giter VIP home page Giter VIP logo

categorydemo's Introduction

#

CategoryDemo Readme

##一、问题:给分类(category)添加属性

  1. 最近遇到一个问题:需要在一个类的Category中添加属性;
  2. OC的分类允许给分类添加属性,但不会自动生成getter、setter方法;
  3. 可以通过 Category 给一个现有的类添加属性,但是却不能添加实例变量
  4. 解决方案:通过runtime建立关联引用;

##二、解决:runtime ###1.引入runtime头文件

#import <objc/runtime.h>

###2.添加属性 可以在分类(即.m文件)中添加,也可以在分类的头文件(即.h文件)中添加。

@interface UIView (EmptyView)

@property (nonatomic, strong) UIButton *hideButton;

@end

###3.实现getter、setter ####1).在implementation中添加属性的getter和setter方法。

//getter 
- (UIButton *)hideButton {
    UIButton *_hideButton = objc_getAssociatedObject(self, @selector(hideButton));
    if (!_hideButton) {
        _hideButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _hideButton.frame = CGRectMake(self.bounds.size.width/2-110, 260, 220, 44);
        _hideButton.backgroundColor = [UIColor brownColor];
        [_hideButton setTitle:@"Hide" forState:UIControlStateNormal];
        objc_setAssociatedObject(self, @selector(hideButton), _hideButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _hideButton;
}

//setter
- (void)setHideButton:(UIButton *)hideButton {
    objc_setAssociatedObject(self, @selector(hideButton), hideButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

####2).在hideButton中使用的objc_getAssociatedOject方法,Object-C中描述如下:

/** 
 * Returns the value associated with a given object for a given key.
 * 
 * @param object The source object for the association.
 * @param key The key for the association.
 * 
 * @return The value associated with the key \e key for \e object.
 * 
 * @see objc_setAssociatedObject
 */
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0);

对于第二个参数const void *key,有以下四种推荐的key值:

  • 声明 static char kAssociatedObjectKey; ,使用 &kAssociatedObjectKey 作为 key 值;
  • 声明 static void *kAssociatedObjectKey = &kAssociatedObjectKey;,使用 kAssociatedObjectKey 作为key值;
  • selector ,使用 getter 方法的名称作为key值;
  • 而使用_cmd可以直接使用该@selector的名称,即hideButton,并且能保证改名称不重复。(与上一种方法相同)

####3).在setHideButton中使用的objc_setAssociatedObject方法,Object-C中描述如下:

/** 
 * Sets an associated value for a given object using a given key and association policy.
 * 
 * @param object The source object for the association.
 * @param key The key for the association.
 * @param value The value to associate with the key key for object. Pass nil to clear an existing association.
 * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
 * 
 * @see objc_setAssociatedObject
 * @see objc_removeAssociatedObjects
 */
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0);

参数说明:

  • objectkey同于objc_getAssociatedObject;
  • value:需要和object建立关联引用对象的value;
  • policy:关联策略,等同于给property添加关键字,具体说明如下表关联策略
**关联策略**

|关联策略 |等价属性|说明| |------|------|------|------| |OBJC_ASSOCIATION_ASSIGN| @property (assign) or @property (unsafe_unretained)| 弱引用关联对象| |OBJC_ASSOCIATION_RETAIN_NONATOMIC| @property (strong, nonatomic)| 强引用关联对象,且为非原子操作| |OBJC_ASSOCIATION_COPY_NONATOMIC| @property (copy, nonatomic)| 复制关联对象,且为非原子操作| |OBJC_ASSOCIATION_RETAIN| @property (strong, atomic)| 强引用关联对象,且为原子操作 |OBJC_ASSOCIATION_COPY| @property (copy, atomic)| 复制关联对象,且为原子操作|

###4.对添加的属性操作 例如将添加的hideButton属性添加到View中

- (void)showHideButton {
	if (!self.hideButton.superview) {
        [self addSubview:self.hideButton];
    }
}

参考资料

  1. Objective-C Associated Objects 的实现原理;
  2. 给分类(Category)添加属性;
  3. iOS Category中添加属性和成员变量的区别

categorydemo's People

Contributors

jixinzhang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.