What does @objc do?

Asked on 05/16/2025

1 search

The @objc attribute in Swift is used to expose Swift code to Objective-C. It allows Swift methods, properties, and classes to be accessible from Objective-C code. This is particularly useful when you are working with legacy Objective-C codebases or when you need to interact with Objective-C frameworks.

When you mark a Swift method or property with @objc, it becomes available to the Objective-C runtime, which means it can be called from Objective-C code, and it can also be used in places where Objective-C dynamic features are required, such as with selectors or when using certain Cocoa APIs that rely on Objective-C runtime features.

In the context of the WWDC sessions, there is a mention of marking methods as OBC direct to allow inlining of Objective-C method calls, which helps reduce retain and release traffic. This is related to performance optimizations when working with Objective-C and Swift interoperability.