| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- //
- // NSMutableDictionary+NullSaf.m
- // EPurse
- //
- // Created by Apple on 2018/1/23.
- // Copyright © 2018年 北风小南巷. All rights reserved.
- //
- #import "NSMutableDictionary+NullSaf.h"
- #import<objc/runtime.h>
- @implementation NSMutableDictionary (NullSaf)
- - (void)swizzeMethod:(SEL)origSelector withMethod:(SEL)newSelector
- {
-
- Class class = [self class];
-
- Method originalMethod = class_getInstanceMethod(class, origSelector);//Method是运行时库的类
-
- Method swizzledMethod = class_getInstanceMethod(class, newSelector);
-
- BOOL didAddMethod = class_addMethod(class, origSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
-
- if (didAddMethod) {
-
- class_replaceMethod(class, newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
-
- }else{
-
- method_exchangeImplementations(originalMethod, swizzledMethod);
- }
-
- }
- - (void)safe_setObject:(id)value forKey:(NSString* )key{
-
- if (value) {
-
- [self safe_setObject:value forKey:key];
-
- }else{
-
- //NSLog(@"[NSMutableDictionary setObject: forKey:%@]值不能为空;",key);
-
- }
-
- }
- - (void)safe_removeObjectForKey:(NSString *)key{
-
- if ([self objectForKey:key]) {
-
- [self safe_removeObjectForKey:key];
-
- }else{
-
- //NSLog(@"[NSMutableDictionary setObject: forKey:%@]值不能为空;",key);
-
- }
-
- }
- + (void)load{
-
- static dispatch_once_t onceToken;
-
- dispatch_once(&onceToken, ^{
-
- id obj = [[self alloc]init];
-
- [obj swizzeMethod:@selector(setObject:forKey:) withMethod:@selector(safe_setObject:forKey:)];
-
- [obj swizzeMethod:@selector(removeObjectForKey:) withMethod:@selector(safe_removeObjectForKey:)];
-
- });
-
- }
- @end
|