实用分类

拦截所有UIButton事件,给UIControl添加分类

#import "UIControl+MonitorAllEvent.h"
#import 
@implementation UIControl (MonitorAllEvent)
+ (void)load{
    
    Method method1 = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method method2 = class_getInstanceMethod(self, @selector(ld_sendAction:to:forEvent:));
    method_exchangeImplementations(method1, method2);
    
}

- (void)ld_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{

    if ([self isKindOfClass:[UIButton class]]) {
        //拦截所有UIButton的事件
        //还可以再区分UIEvent事件
        //做完自己的事情之后
        //调用原来的实现,方法已经交换,所以调用的是ld_sendAction方法在世调用系统的方法
        //[self sendAction:action to:target forEvent:event];
        [self ld_sendAction:action to:target forEvent:event];
    }
}

处理NSObject的Crash事件

#import "NSObject+NSObjectCrash.h"

@implementation NSObject (NSObjectCrash)
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    //可以正常调用的方法
    if ([self respondsToSelector:aSelector]) {
        return [super methodSignatureForSelector:aSelector];
    }
    
    //不能正常调用的方法
    return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation{
    NSLog(@"%@类的%@方法找不到",[self class],NSStringFromSelector(anInvocation.selector));
}
@end

可变数组元素不为空

#import "NSMutableArray+NSMutableArrayNotNil.h"
#import 
@implementation NSMutableArray (NSMutableArrayNotNil)
+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 类簇:NSString、NSArray、NSDictionary,真实类型是其他类型

        Class cls = NSClassFromString(@"__NSArrayM");
        Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));
        Method method2 = class_getInstanceMethod(cls, @selector(ld_insertObject:atIndex:));
        
        method_exchangeImplementations(method1, method2);
    });

    
}
- (void)ld_insertObject:(id)anObject atIndex:(NSUInteger)index{
    //如果元素为nil,返回不插入
    if (anObject == nil) return;
    [self ld_insertObject:anObject atIndex:index];
}
@end

字典Key不为nil

#import "NSMutableDictionary+Extension.h"
#import 

@implementation NSMutableDictionary (Extension)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls = NSClassFromString(@"__NSDictionaryM");
        Method method1 = class_getInstanceMethod(cls, @selector(setObject:forKeyedSubscript:));
        Method method2 = class_getInstanceMethod(cls, @selector(ld_setObject:forKeyedSubscript:));
        method_exchangeImplementations(method1, method2);
        
        Class cls2 = NSClassFromString(@"__NSDictionaryI");
        Method method3 = class_getInstanceMethod(cls2, @selector(objectForKeyedSubscript:));
        Method method4 = class_getInstanceMethod(cls2, @selector(ld_objectForKeyedSubscript:));
        method_exchangeImplementations(method3, method4);
    });
}

- (void)ld_setObject:(id)obj forKeyedSubscript:(id)key
{
    if (!key) return;
    
    [self ld_setObject:obj forKeyedSubscript:key];
}

- (id)ld_objectForKeyedSubscript:(id)key
{
    if (!key) return nil;
    
    return [self ld_objectForKeyedSubscript:key];
}

@end

HOOK [UIFont systemFontOfSize:15]方法可以修改全局字体

你可能感兴趣的:(实用分类)