TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
@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.
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,@objcprotocols) automatically receive the@objcattribute. - Access Control Exceptions: The
@objcMembersattribute explicitly ignoresprivateandfileprivatemembers. Even if a private member is Objective-C compatible, it will not receive the implicit@objcattribute. 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
@objcattribute and remain hidden from Objective-C.
Inheritance and Extensions
The@objcMembers attribute propagates down the inheritance hierarchy and across extensions:
- Subclasses: Any class that inherits from an
@objcMembersclass automatically inherits the@objcMembersbehavior. All eligible members of the subclass are implicitly exposed to Objective-C. - 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.
Master Swift with Deep Grasping Methodology!Learn More





