Title: RunTime 之方法交换 · Issue #4 · aTreey/aTreey.github.io · GitHub
Open Graph Title: RunTime 之方法交换 · Issue #4 · aTreey/aTreey.github.io
X Title: RunTime 之方法交换 · Issue #4 · aTreey/aTreey.github.io
Description: 以前只是看到过运行时相关的资料,现在项目中依然在用UIWebView 最近打算给WebView 加一个假的进度条,所以打算通过使用RunTime给UIWebView 增加分类关联属性和方法的方式实现 方法交换 class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name) 获取cls类的实例方法, cls 要获取实例方法的相关类, name 需要获取类的实例方法名称 class_getClassMethod...
Open Graph Description: 以前只是看到过运行时相关的资料,现在项目中依然在用UIWebView 最近打算给WebView 加一个假的进度条,所以打算通过使用RunTime给UIWebView 增加分类关联属性和方法的方式实现 方法交换 class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name) 获取cls类的实例方法, cls 要获取实例方法的相关类...
X Description: 以前只是看到过运行时相关的资料,现在项目中依然在用UIWebView 最近打算给WebView 加一个假的进度条,所以打算通过使用RunTime给UIWebView 增加分类关联属性和方法的方式实现 方法交换 class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name) 获取cls类的实例方法, cls 要获取实例方法的相关类...
Opengraph URL: https://github.com/aTreey/aTreey.github.io/issues/4
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"RunTime 之方法交换","articleBody":"以前只是看到过运行时相关的资料,现在项目中依然在用`UIWebView` 最近打算给`WebView` 加一个假的进度条,所以打算通过使用`RunTime`给`UIWebView` 增加分类关联属性和方法的方式实现\r\n## 方法交换\r\n\r\n- `class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)` 获取`cls`类的实例方法,\r\n - `cls` 要获取实例方法的相关类,\r\n - `name` 需要获取类的实例方法名称\r\n\r\n- `class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)` 获取`cls`类的类方法,\r\n - `cls` 要获取实例方法的相关类,\r\n - `name` 需要获取类的类方法名称\r\n\r\n- `method_getImplementation` 获取一个方法的`IMP`(方法的实现),具体是指`implement`\r\n\r\n- `method_getTypeEncoding ` 获取一个方法实现的Type字符串,包括参数类型和返回值类型\r\n\r\n- `class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, \r\n const char * _Nullable types)` 添加一个新的方法,\r\n - `cls`: 需要被添加的类\r\n - `SEL`: 要被添加的方法 例如:`@selector(webViewDidFinishLoad:)`\r\n - `IMP`: 添加的方法实现地址,例如:`(IMP)my_webViewDidFinishLoad`\r\n - `types`: 方法实现的Type字符串,包括参数类型和返回值类型\r\n\r\n\r\n \u003e 无参无返回值: \"V@:\"\r\n \u003e 有参无返回值: \"i@:\" 返回值为`int`类型\r\n \u003e 有参有返回值: \"i@:@\" 返回值为`int`类型,有一个参数\r\n\r\n \r\n - 返回值为`BOOl` \r\n - 保证即便originalSelector只在父类中实现,也能达到Method Swizzling的目的, 比如一个方法只是在父类中实现而在子类中未实现,`class_addMethod`就会执行返回值为`YES`以达到方法替换,\r\n - 如果子类本身实现了这个方法,返回值为NO,然后我们只需要直接执行`class_replaceMethod`方法就可以达到目的\r\n\r\n- `class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, \r\n const char * _Nullable types)` 替换类中添加成功的方法的实现, 如果该方法不存在就会添加该方法, 覆盖执行`class_addMethod`然后再替换换两个方法实现\r\n\r\n- `method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2) ` 交换两个已经存在的方法实现\r\n\r\n[交换方法代码示例参考](http://nshipster.com/method-swizzling/)\r\n\r\n\r\n## 给分类关联属性和方法\r\n\r\n- `objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key) ` 给get方法关联值,返回与给定键的特定对象关联的值\r\n\r\n- `objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,\r\n id _Nullable value, objc_AssociationPolicy policy)` 给`set`方法关联值,\r\n - object:要关联的**源对象**\r\n - key:关联时标记是哪一个属性的key,用于区分关联的多个属性,可以用 `_cmd`代替, `_cmd` 在Objective-C的方法中表示当前方法的selector,正如同self表示当前方法调用的对象实例一样,每个方法都是不一样的\r\n - value: 关联的对象\r\n - policy:关联策略\r\n\r\n","author":{"url":"https://github.com/aTreey","@type":"Person","name":"aTreey"},"datePublished":"2018-06-05T15:47:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/4/aTreey.github.io/issues/4"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ed1b1544-12a9-202f-b32a-a870db9a05c6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9D54:0B26:912110:C6FB2D:6A570846 |
| html-safe-nonce | de75fee9cc8d5f78d44a2a0b4e2cfbfd532bd88451581aa045b6e706a2689dca |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDU0OjBCMjY6OTEyMTEwOkM2RkIyRDo2QTU3MDg0NiIsInZpc2l0b3JfaWQiOiIzNjIzODA5ODMwOTMzMTcwMjQ3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c94ae82a95653132bdd4293a2b7fc0d686597b7912ebd557946275a6d6fee670 |
| hovercard-subject-tag | issue:329517842 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/aTreey/aTreey.github.io/4/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1108099b5abda841c32f3676214c7c0a797a1864ce84f8e0b415165e81dba0b7/aTreey/aTreey.github.io/issues/4 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1108099b5abda841c32f3676214c7c0a797a1864ce84f8e0b415165e81dba0b7/aTreey/aTreey.github.io/issues/4 |
| og:image:alt | 以前只是看到过运行时相关的资料,现在项目中依然在用UIWebView 最近打算给WebView 加一个假的进度条,所以打算通过使用RunTime给UIWebView 增加分类关联属性和方法的方式实现 方法交换 class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name) 获取cls类的实例方法, cls 要获取实例方法的相关类... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aTreey |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb29e5f807bd95e41196adfbc6d28f9af12d89830d8b684f3e871774ddff2fc |
| turbo-cache-control | no-preview |
| go-import | github.com/aTreey/aTreey.github.io git https://github.com/aTreey/aTreey.github.io.git |
| octolytics-dimension-user_id | 14048652 |
| octolytics-dimension-user_login | aTreey |
| octolytics-dimension-repository_id | 125624771 |
| octolytics-dimension-repository_nwo | aTreey/aTreey.github.io |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 125624771 |
| octolytics-dimension-repository_network_root_nwo | aTreey/aTreey.github.io |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b056788a129fb0194b61f2ebb4c43f6a9f4dfd27 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width