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

前端之家收集整理的这篇文章主要介绍了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运行时以动态调度调用.

猜你在找的C&C++相关文章