微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – Swift 3 – 动态vs @objc

方法标记为@objc与动态之间有什么区别?什么时候做一个vs另一个

以下是Apple的动态定义.

dynamic Apply this modifier to any member of a class that can be
represented by Objective-C. When you mark a member declaration with
the dynamic modifier,access to that member is always dynamically
dispatched using the Objective-C runtime. Access to that member is
never inlined or devirtualized by the compiler.

Because declarations marked with the dynamic modifier are dispatched
using the Objective-C runtime,they’re implicitly marked with the objc
attribute.

解决方法

声明为@objc的函数/变量可从Objective-C访问,但 Swift将通过静态或虚拟调度继续访问它.
这意味着如果函数/变量通过Objective-C框架旋转,就像使用键值观察或各种Objective-C API修改类时会发生什么,调用Swift和Objective-C的方法会产生不同的结果.

使用动态告诉Swift总是引用Objective-C动态调度.这是像键值观察这样的工作所必需的.当Swift函数调用时,它指的是Objective-C运行时以动态调度调用.

原文地址:https://www.jb51.cc/c/114521.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐