0
点赞
收藏
分享

微信扫一扫

ios给系统添加分类管理属性

爱写作的小土豆 2022-02-07 阅读 79
iosswift

ios给系统添加分类管理属性

swift 测试

import UIKit
import ObjectiveC

struct MDTableConst{
    static let associatedKey = UnsafeRawPointer.init(bitPattern: "MDTableAccessoryKey".hashValue)
}
public extension UITableView{
    /*var manager:TableManager?{
        get{
            return objc_getAssociatedObject(self,MDTableConst.associatedKey!) as? TableManager
        }set{
            newValue?.bindTo(tableView: self)
            objc_setAssociatedObject(self, MDTableConst.associatedKey!, newValue, .OBJC_ASSOCIATION_RETAIN)
        }
    }*/
}

oc

@interface UIViewController (AddPropertyInCategory)
@property(nonatomic,assign)id  test_assign;
@property(nonatomic,strong)UIView *testView_Strong;
@end
#import "UIViewController+AddPropertyInCategory.h"
#import <objc/runtime.h>
static const void *test_assginKey = &test_assginKey;
static const void *test_strongKey = &test_strongKey;
@implementation UIViewController (AddPropertyInCategory)
- (id)test_assign{
    return objc_getAssociatedObject(self, test_assginKey);
}

- (void)setTest_assign:(id)test_assign{
    objc_setAssociatedObject(self, test_assginKey, test_assign, OBJC_ASSOCIATION_ASSIGN);
}

- (UIView *)testView_Strong{
    return  objc_getAssociatedObject(self, test_strongKey);
}
- (void)setTestView_Strong:(UIView *)testView_Strong{
    objc_setAssociatedObject(self, test_strongKey, testView_Strong, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end

调用

#import "Test1VC.h"
#import "UIViewController+AddPropertyInCategory.h"

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    Test1VC *testVc = [[Test1VC alloc]init];
    UIView *iv = [[UIView alloc] init];
    iv.backgroundColor = UIColor.redColor;
    testVc.testView_Strong = iv;
}

举报

相关推荐

0 条评论