Swift 3 - dynamic vs @objc

前端 未结 3 739
轮回少年
轮回少年 2020-12-13 17:25

What\'s the difference between marking a method as @objc vs dynamic, when would you do one vs the other?

Below is Apple\'s definition for dynamic.

3条回答
  •  不思量自难忘°
    2020-12-13 17:56

    As that quote says, dynamic implies @objc.

    Unless you specify a class as being dynamic, the compiler is free to optimize away and inline its methods. This brings huge performance benefits, but it means that you can't change those method implementations at run time. If you're planning to mess around with those methods at runtime using the reflection capabilities of the Objective C runtime, you'll need to use dynamic. You'll incur a performance penalty (your code will run at Objective C levels of speed, rather than near C-like levels), but you'll gain that extra dynamism.

提交回复
热议问题