ios Method Swizzling学习笔记
2016-04-14 17:57
381 查看
Method Swizzling我的理解是运用Runtime获取类与类之间的方法,并且可以进行对换,从而达到某些目的,但是通过学习感觉和继承或者分类上使用父类的方法差不多,这种方式去搞,反而有点让接手的人不知所然,只是让外人觉得略微显得有点逼格一点。运行时可以对分类增加属性,在SDImageView中有大量使用,正常在分类中增加属性是无效的
objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
学习代码创建三个类,
FOO、Bar、BarCategatory
main行数测试
objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
学习代码创建三个类,
FOO、Bar、BarCategatory
#import <Foundation/Foundation.h> @interface Foo : NSObject - (void) testMethod; - (void) baseMethod; - (void) recursionMethod; + (BOOL)swizzleMethod:(SEL)origSel withMethod:(SEL)altSel; + (BOOL)swizzleClassMethod:(SEL)origSel withClassMethod:(SEL)altSel; @end
// // Foo.m // AuditionTest // // Created by xie on 16/4/14. // Copyright © 2016年 CJH. All rights reserved. // #import "Foo.h" #import <objc/runtime.h> @implementation Foo - (void) testMethod { NSLog(@" >> Foo testMethod"); } - (void) baseMethod { NSLog(@" >> Foo baseMethod"); } - (void) recursionMethod { NSLog(@" >> Foo recursionMethod"); } //交换方法 + (BOOL)swizzleMethod:(SEL)origSel withMethod:(SEL)altSel { Method origMethod = class_getInstanceMethod(self, origSel); if (!origSel) { NSLog(@"original method %@ not found for class %@", NSStringFromSelector(origSel), [self class]); return NO; } Method altMethod = class_getInstanceMethod(self, altSel); if (!altMethod) { NSLog(@"original method %@ not found for class %@", NSStringFromSelector(altSel), [self class]); return NO; } class_addMethod(self, origSel, class_getMethodImplementation(self, origSel), method_getTypeEncoding(origMethod)); class_addMethod(self, altSel, class_getMethodImplementation(self, altSel), method_getTypeEncoding(altMethod)); method_exchangeImplementations(class_getInstanceMethod(self, origSel), class_getInstanceMethod(self, altSel)); return YES; } + (BOOL)swizzleClassMethod:(SEL)origSel withClassMethod:(SEL)altSel { Class c = object_getClass((id)self); return [c swizzleMethod:origSel withMethod:altSel]; } @end
#import "Foo.h" @interface Bar : Foo - (void) testMethod; @end
#import "Bar.h" @implementation Bar - (void) testMethod { NSLog(@" >> Bar testMethod"); } @end
#import "Bar.h" @interface Bar (BarCategory) - (void) altTestMethod; - (void) altBaseMethod; - (void) altRecursionMethod; @end
#import "Bar+BarCategory.h" @implementation Bar (BarCategory) - (void) altTestMethod { NSLog(@" >> Bar(BarCategory) altTestMethod"); } - (void) altBaseMethod { NSLog(@" >> Bar(BarCategory) altBaseMethod"); } - (void) altRecursionMethod { NSLog(@" >> Bar(BarCategory) recursionMethod"); [self altRecursionMethod]; } @end
main行数测试
#import <UIKit/UIKit.h> #import "AppDelegate.h" #import "Foo.h" #import "Bar+BarCategory.h" #import "Bar.h" int main(int argc, char * argv[]) { @autoreleasepool { @autoreleasepool { Foo * foo = [[Foo alloc] init]; Bar * bar = [[Bar alloc] init]; NSLog(@"========= Method Swizzling test 1 ========="); NSLog(@" Step 1"); [foo testMethod]; [bar testMethod]; [bar altTestMethod]; NSLog(@" Step 2"); [Bar swizzleMethod:@selector(testMethod) withMethod:@selector(altTestMethod)]; [foo testMethod]; [bar testMethod]; [bar altTestMethod]; NSLog(@"========= Method Swizzling test 2 ========="); NSLog(@" Step 1"); [foo baseMethod]; [bar baseMethod]; [bar altBaseMethod]; NSLog(@" Step 2"); [Bar swizzleMethod:@selector(baseMethod) withMethod:@selector(altBaseMethod)]; [foo baseMethod]; [bar baseMethod]; [bar altBaseMethod]; NSLog(@"========= Method Swizzling test 3 ========="); [Bar swizzleMethod:@selector(recursionMethod) withMethod:@selector(altRecursionMethod)]; [bar recursionMethod]; } return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Ruby简明教程之方法(Method)介绍
- IE7下ajax之open Method New的说明
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- PHP中魔术变量__METHOD__与__FUNCTION__的区别
- 探讨Android与iOS,我们将何去何从?