iosRuntimeiOS开发攻城狮的集散地

iOS 给分类category添加属性

2016-12-07  本文已影响2295人  Jixin

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

  1. 最近遇到一个问题:需要在一个类的Category中添加属性;
  2. 可以通过 Category 给一个现有的类添加属性,但是却不能添加实例变量;
  3. 解决方案:通过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值:

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);

参数说明:

关联策略

|关联策略 |等价属性|说明|
|------|------|------|------|
|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. <a href = http://blog.leichunfeng.com/blog/2015/06/26/objective-c-associated-objects-implementation-principle>Objective-C Associated Objects 的实现原理</a>;
  2. <a href = http://www.jianshu.com/p/3cbab68fb856>给分类(Category)添加属性</a>;
  3. <a href = http://www.jianshu.com/p/535d1574cb86>iOS Category中添加属性和成员变量的区别</a>。

Github链接

<a href = https://github.com/JixinZhang/CategoryDemo/tree/master>查看代码请点击这里</a>

上一篇下一篇

猜你喜欢

热点阅读