I tried to replace the dictionary's setObject:<#(nonnull id)#> forKey:<#(nonnull id<NSCopying>)#>
method with the runtime, but it failed.
Code:
#import "NSMutableDictionary+MD5MutableDictionary.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (MD5MutableDictionary)
+(void)load
{
Method A = class_getInstanceMethod(self, @selector(setObject:forKey:));
Method B = class_getInstanceMethod(self ,@selector(testsetObject:forKey:));
method_exchangeImplementations(A, B);
}
- (void)testsetObject:(id)anObject forKey:(id<NSCopying>)aKey{
[self testsetObject:anObject forKey:aKey];
NSLog(@"success");
}
Called directly outside through a variable dictionary object for example:
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"jack" forKey:@"name"];