是否有一个 Swift 属性,例如为协议设计的@nonobjc?
Is there a Swift attribute, such as @nonobjc designed for protocols?
我想写一个 PAT,我不关心 Obj-C 互操作性。 属性听起来很完美,但它仅适用于变量和方法。
任何类似的隐藏 Obj-C 协议的东西?
您似乎误解了 @nonobjc
属性的用途:
来自docs:
nonobjc
Apply this attribute to a method, property, subscript, or initializer
declaration to suppress an implicit objc
attribute.
如果您进一步向下滚动页面,它会告诉您哪些内容将具有隐式 objc
属性:
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.
这不包括协议,因此协议永远不会隐式公开给 Objective-C。这意味着您不需要协议上的 nonobjc
属性来抑制协议上的隐式 objc
。默认情况下,协议不会暴露给 Objective-C,除非您用 @objc
.
标记它们
我想写一个 PAT,我不关心 Obj-C 互操作性。
您似乎误解了 @nonobjc
属性的用途:
来自docs:
nonobjc
Apply this attribute to a method, property, subscript, or initializer declaration to suppress an implicit
objc
attribute.
如果您进一步向下滚动页面,它会告诉您哪些内容将具有隐式 objc
属性:
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
, orGKInspectable
attribute.
这不包括协议,因此协议永远不会隐式公开给 Objective-C。这意味着您不需要协议上的 nonobjc
属性来抑制协议上的隐式 objc
。默认情况下,协议不会暴露给 Objective-C,除非您用 @objc
.