When you mark a member declaration with the
dynamic
modifier,access to that member is always dynamically dispatched. Because declarations marked with thedynamic
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
,andC
,whereB
andC
both inherit the methodfoo()
fromA
. Now supposex
is a variable of classA
. At run time,x
may actually have a value of typeB
orC
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) }
我以为我错过了一些东西,但是我不明白在运行时调用的多态操作(方法或函数)在哪里.
这里动态调度的目的是什么?