Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The @objcMembers attribute is a class-level modifier in Swift that implicitly applies the @objc attribute to eligible Objective-C compatible members of a class, its extensions, its subclasses, and all of their extensions. It instructs the compiler to expose these declarations to the Objective-C runtime by generating Objective-C thunks, enabling Objective-C message passing without requiring explicit @objc annotations on every individual property, method, or initializer. Crucially, this implicit behavior explicitly excludes members with private or fileprivate access control.
@objcMembers
class Entity: NSObject {
    var name: String = "" // Implicitly @objc
    func update() {}      // Implicitly @objc
    
    private func internalUpdate() {} // NOT @objc due to access control
    @objc private func tapHandler() {} // Explicitly @objc
}

Dispatch Mechanics

Applying @objcMembers (or @objc) exposes members to the Objective-C runtime but does not alter how Swift dispatches calls to those members. Swift continues to use standard vtable (virtual) or static dispatch for Swift-to-Swift calls. To force dynamic dispatch via Objective-C message passing within Swift (which is required for features like Key-Value Observing or method swizzling), the dynamic modifier must be explicitly applied to the specific member.

Technical Mechanics

When @objcMembers is applied to a class, the Swift compiler evaluates every member to determine if it receives the implicit @objc attribute based on the following rules:
  • Objective-C Compatibility: The class itself must inherit from an Objective-C class (typically NSObject).
  • Implicit Bridging: Properties, methods, and initializers utilizing Swift types that bridge to Objective-C (e.g., String, Int, Array, @objc protocols) automatically receive the @objc attribute.
  • Access Control Exceptions: The @objcMembers attribute explicitly ignores private and fileprivate members. Even if a private member is Objective-C compatible, it will not receive the implicit @objc attribute. If a private member requires Objective-C runtime visibility (such as a target-action method referenced via #selector), it must be explicitly annotated with @objc.
  • Swift-Exclusive Types: Members utilizing Swift-only features—such as tuples, generics, structs, or enums with associated values—are strictly incompatible with the Objective-C runtime. The compiler silently ignores these members; they do not receive the implicit @objc attribute and remain hidden from Objective-C.

Inheritance and Extensions

The @objcMembers attribute propagates down the inheritance hierarchy and across extensions:
  1. Subclasses: Any class that inherits from an @objcMembers class automatically inherits the @objcMembers behavior. All eligible members of the subclass are implicitly exposed to Objective-C.
  2. Extensions: Members defined within extensions of the annotated class, or extensions of its subclasses, are also implicitly marked as @objc.

Opting Out with @nonobjc

Because @objcMembers applies a blanket exposure to the Objective-C runtime, it generates Objective-C thunks for all eligible members, which increases the compiled binary size. To override this implicit behavior for specific members, you must use the @nonobjc attribute. This explicitly hides the member from the Objective-C runtime.
@objcMembers
class Configuration: NSObject {
    // Implicitly @objc (Exposed to Objective-C runtime)
    var timeout: Double = 30.0
    
    // Implicitly @objc
    func reset() {}
    
    // Ignored by @objcMembers (Tuple is a Swift-only type)
    var metadata: (Int, String) = (0, "none")
    
    // Ignored by @objcMembers due to 'private' access control
    private func calculateHash() {}
    
    // Explicitly @objc despite 'private' access control (e.g., for #selector)
    @objc private func handleTimeout() {}
    
    // Explicitly excluded from Objective-C runtime
    @nonobjc func performInternalRouting() {}
    
    // Explicitly marked dynamic to force Objective-C message passing in Swift
    dynamic func observableMethod() {}
}

// Inherits @objcMembers behavior
class NetworkConfiguration: Configuration {
    // Implicitly @objc
    var endpoint: String = ""
}
Master Swift with Deep Grasping Methodology!Learn More