Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - dynamic vs @objc

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.

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.

like image 777
Boon Avatar asked Nov 24 '16 23:11

Boon


People also ask

What is @objc dynamic in Swift?

Swift and Objective-C Interoperability Dynamic what? Dynamic dispatch. It simply means that the Objective-C runtime decides at runtime which implementation of a particular method or function it needs to invoke.

What is the use of @objc in Swift?

That's where the @objc attribute comes in: when you apply it to a class or method it instructs Swift to make those things available to Objective-C as well as Swift code.

What are the differences between implementing a @property with @dynamic or synthesize?

@synthesize will generate getter and setter methods for your property. @dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime).

Why do we need to add @objc in the selector function?

You must have noticed that we need to add the prefix @objc for #selector(function) in case of adding target to Button, UITabGestureRecogniser or Timer. This is because the Selector is a C string that represents the name of the method at the runtime.


2 Answers

A function/variable declared as @objc is accessible from Objective-C, but Swift will continue to access it directly via static or virtual dispatch. This means if the function/variable is swizzled via the Objective-C framework, like what happens when using Key-Value Observing or the various Objective-C APIs to modify classes, calling the method from Swift and Objective-C will produce different results.

Using dynamic tells Swift to always refer to Objective-C dynamic dispatch. This is required for things like Key-Value Observing to work correctly. When the Swift function is called, it refers to the Objective-C runtime to dynamically dispatch the call.

like image 69
kevin Avatar answered Oct 03 '22 22:10

kevin


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.

like image 31
Alexander Avatar answered Oct 03 '22 23:10

Alexander