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

ios – Swift函数中的动态修改器,第1张

概述据苹果: 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 th 据苹果:

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 – Swift函数中的动态修改器全部内容,希望文章能够帮你解决ios – Swift函数中的动态修改器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110268.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存