Search code examples
swiftlanguage-interoperability

Is there a Swift attribute, such as @nonobjc designed for protocols?


I want to write a PAT and I don't care about Obj-C interoperability. The @nonobjc attribute sounds perfect but its designed for variables and methods only. Anything similar for hiding protocols from Obj-C?


Solution

  • You seem to be misunderstood what the @nonobjc attribute is for:

    From the docs:

    nonobjc

    Apply this attribute to a method, property, subscript, or initializer declaration to suppress an implicit objc attribute.

    If you scroll further down the page, it tells you what will have an implicit objc attribute on them:

    The compiler implicitly adds the objc attribute to subclasses of any class defined in Objective-C. However, the subclass must not be generic, and must not inherit from any generic classes. [...] The objc attribute is also implicitly added in the following cases:

    • The declaration is an override in a subclass, and the superclass’s declaration has the objc attribute.
    • The declaration satisfies a requirement from a protocol that has the objc attribute.
    • The declaration has the IBAction, IBSegueAction, IBOutlet, IBDesignable, IBInspectable, NSManaged, or GKInspectable attribute.

    This does not include protocols, so protocols are never implicitly exposed to Objective-C. This means that you don't need the nonobjc attribute on protocols to suppress implicit objcs on protocols. Protocols, by default, are not exposed to Objective-C, unless you mark them with @objc.