When @objc and @nonobjc write before method and variable in swift?

后端 未结 3 580
时光说笑
时光说笑 2021-02-04 06:28

When I declare static parameter in extension of class then I have to write @nonobjc before variable like

 @nonobjc static let test = \"test\"

a

3条回答
  •  一生所求
    2021-02-04 07:11

    (Addendum/additional official details to @bontoJR well summarizing answer)

    From the Swift Language Reference - Attributes [emphasis mine]:

    objc

    Apply this attribute to any declaration that can be represented in Objective-C — for example, non-nested classes, protocols, nongeneric enumerations (constrained to integer raw-value types), properties and methods (including getters and setters) of classes and protocols, initializers, deinitializers, and subscripts. The objc attribute tells the compiler that a declaration is available to use in Objective-C code.

    ...

    nonobjc

    Apply this attribute to a method, property, subscript, or initializer declaration to suppress an implicit objc attribute. The nonobjc attribute tells the compiler to make the declaration unavailable in Objective-C code, even though it is possible to represent it in Objective-C.

    ...

提交回复
热议问题