Last active
May 21, 2019 08:56
-
-
Save anddygon/76a7abaab3170401c6871bc9ae391d46 to your computer and use it in GitHub Desktop.
利用Extension“重载”原有实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 先了解下Module在iOS中的概念 | |
| // Module,中文译为「标准部件;模块」。iOS 中的 Module 类似于 Java 中的 Jar 包,也可以称作第三方库,或者框架(Framework)。 | |
| // 假设一个场景 A项目用cocoapods使用了B框架,这时候项目就2个module,A B,我们想修改B项目中一个公开方法的实现 | |
| // 方案一: fork自己任意修改,这样可以达到目的,但是一来费事,二来可能这么做不一定合理 | |
| // 方案二: 利用iOS访问控制,当我们在A module里面调用B里面的一个方法,系统会首先在当前Module里查找要调用方法所属类的extension里的同名方法, | |
| // 这话有点绕,上代码吧 | |
| // A 中 | |
| extention BClass { | |
| public func bFunc() { | |
| print(“新实现”) | |
| } | |
| } | |
| B 中 | |
| class BClass { | |
| public func bFunc() { | |
| print(“原有实现”) | |
| } | |
| } | |
| // 这时候会发现系统会走 新实现 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment