zoukankan      html  css  js  c++  java
  • iOS Runtime学习笔记

    Associated Objects:

    @interface NSObject (AssociatedObject)
    @property (nonatomic, strong) id associatedObject;
    @end
    @implementation NSObject (AssociatedObject)
    @dynamic associatedObject;
    
    - (void)setAssociatedObject:(id)object {
         objc_setAssociatedObject(self, @selector(associatedObject), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (id)associatedObject {
        return objc_getAssociatedObject(self, @selector(associatedObject));
    }

    Swizzling: 

    + (void)load{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = [self class];
            SEL originalSEL = @selector(viewWillAppear:);
            SEL swizzledSEL = @selector(xxx_viewWillAppear:);
            
            Method originalMethod = class_getInstanceMethod(class, originalSEL);
            Method swizzledMethod = class_getInstanceMethod(class, swizzledSEL);
            
            BOOL didAddMethod = class_addMethod(class, originalSEL, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
            
            if (didAddMethod) {
                class_replaceMethod(class, swizzledSEL, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
            }else{
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
            
        });
    }
    - (void)xxx_viewWillAppear:(BOOL)animated {
        [self xxx_viewWillAppear:animated];
        NSLog(@"viewWillAppear: %@", self);
    }
  • 相关阅读:
    数据库自动备份(转)
    sqlserver常用全局变量
    Remoting通讯实例
    自定义ORM框架(转转)
    带格式导出数据到Excel
    app.config动态修改及读取
    学习笔记之AJAX无刷新分页
    游标(转转)
    Sql Server索引(转载)
    流Stream个人学习理解
  • 原文地址:https://www.cnblogs.com/ficow/p/5383943.html
Copyright © 2011-2022 走看看