ios – Swift函数中的动态修改器

前端之家收集整理的这篇文章主要介绍了ios – Swift函数中的动态修改器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
据苹果:

When you mark a member declaration with the dynamic modifier,access to that member is always dynamically dispatched. Because declarations marked with the dynamic modifier are dispatched using the Objective-C runtime,they’re implicitly marked with the @objc attribute.

根据维基百科:

dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time.

Dynamic dispatch is often used in object-oriented languages when different classes contain different implementations of the same method due to common inheritance. For example,suppose you have classes A,B,and C,where B and C both inherit the method foo() from A. Now suppose x is a variable of class A. At run time,x may actually have a value of type B or C and in general you can’t know what it is at compile time.

现在,我正在研究dependency injection框架:Typhoon,当我在所有继承Objective-C类TyphoonAssembly的类中打开Swift的示例项目时,所有注入依赖关系的方法都有动态修饰符包含在以下方式:

public dynamic func weatherReportDao() -> AnyObject {
    return TyphoonDefinition.withClass(WeatherReportDaoFileSystemImpl.self)
}

我以为我错过了一些东西,但是我不明白在运行时调用的多态操作(方法函数)在哪里.

这里动态调度的目的是什么?

解决方法

你的问题的答案在这篇文章解决

https://github.com/appsquickly/typhoon/wiki/TyphoonAssembly

基本上在运行时,Typhoon Framework将用自己的例程来替换你的方法,实现框架的功能,并调用你的方法来做任何你为它定义的工作.

为了使框架能够替代方法,该方法必须动态调度.

猜你在找的iOS相关文章