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);
    }
  • 相关阅读:
    poj 2481
    poj 3928 Ping pong
    再见oi
    NOIP 2014 解方程
    poj1836:Alignment
    poj2479:Maximum sum
    tyvj1510:专家复仇
    tyvj:P1467 通向聚会的道路
    tyvj1176: 火焰巨魔的惆怅
    tyvj1326:剑人合一
  • 原文地址:https://www.cnblogs.com/ficow/p/5383943.html
Copyright © 2011-2022 走看看