简体   繁体   English

在运行时迅速注入具体类型

[英]inject concrete type at runtime in swift

Is it at all possible to have a concrete type injected at runtime. 在运行时是否有可能注入具体类型。 For example, let's say you have two frameworks that have the same methods with different implementations: 例如,假设您有两个框架,它们的方法相同,但实现方式不同:

FrameworkA
func name() -> String {
  return "A"
}

FrameworkB
func name() -> String {
  return "B"
}

So you create a protocol in FrameworkC 所以你在FrameworkC中创建一个协议

FrameworkC
protocol Namable {
  func name() -> String
}

class SomeOtherClass {
    // uses Namable
}

Then in an actual app that uses FrameworkC, can you embed or link just FrameworkA and then FrameworkA's implementations would be used? 然后,在使用FrameworkC的实际应用中,是否可以仅嵌入或链接FrameworkA,然后使用FrameworkA的实现? And similarly some other consumer of FrameworkC could use FrameworkB's implementations? 同样,FrameworkC的其他使用者也可以使用FrameworkB的实现?

I've done something similar, but not with frameworks per-se. 我做了类似的事情,但是没有使用框架本身。 Imagine you have an app, and you want another app that includes all the code of the first app plus some "tweaks" to the functionality of that code. 假设您有一个应用程序,而您想要另一个应用程序,它包含第一个应用程序的所有代码以及对该代码功能的一些“调整”。 And you want to minimize what has to change in the first (base) app's codebase. 并且您希望最小化第一个(基本)应用程序的代码库中需要更改的内容。 (ie I didn't want to subclass as that will quickly wind up refactoring the base code in many undesireable ways.) (即,我不想子类化,因为那样会很快以许多不希望的方式重构基础代码。)

I've actually successfully written & shipped several iOS apps "atop" another iOS app (where each other app is just a set of tweak code plus the underlying app's code. The base app's code merely had to call some extra methods that do nothing in the base app - methods that conformed to a specific protocol. The base app had no significant change, yet its codebase was tweaked and extended in major ways by another app (actually several) that also compiled in that codebase. 实际上,我已经成功地在另一个iOS应用“之上”编写并交付了多个iOS应用(每个其他应用只是一组调整代码以及底层应用的代码。基本应用的代码只需要调用一些额外的方法即可)基本应用程序-符合特定协议的方法基本应用程序没有重大变化,但是它的代码库由另一个也在同一代码库中编译的应用程序(实际上是几个)进行了重大调整和扩展。

By using protocol extensions and the where clause to target a specific concrete class that implements the protocol, you can achieve targeted "tweaks" of a base set of code (say, of a first app) simply by adding extra code files to the (second) app. 通过使用协议扩展和where子句以实现该协议的特定具体类为目标,您只需将额外的代码文件添加到第二个基本代码集(例如,第一个应用程序)即可实现目标代码“调整” )应用。 It can be a very powerful way to extend the base functionality of a codebase without re-architecting or rearranging the base code at all. 扩展代码库的基本功能可能是一种非常强大的方法,而根本无需重新架构或重新排列基本代码。

So try having Framework A and B both extend your Namable protocol with "where" clause to target a concrete class of Framework C. And Framework C should probably have the same extension too, just with empty no-op methods. 因此,尝试让框架A和框架B都使用“ where”子句扩展您的可命名协议,以针对框架C的具体类。框架C应该也应该具有相同的扩展,只是使用空的无操作方法。 So Framework A or B can basically provide a plugin-able implementation for the empty protocol'd method of Framework C, in a way where Framework C knows nothing about A and B. You can basically target a specific method of a specific class in this way, replacing its contents merely by the presense of other code being linked in (the extension protocol with where clause). 因此,框架A或B基本上可以为框架C的空协议方法提供可插入的实现,而框架C对此一无所知。您基本上可以在此针对特定类的特定方法方式,仅以链接的其他代码(带有where子句的扩展协议)为前提替换其内容。

It's one of the most miraculous things about Swift, IMHO. 这是Swift,恕我直言的最神奇的事情之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM