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

When I declare a static parameter in a class extension, then I need to write @nonobjc before the variable, for example

@nonobjc static let test = "test" 

and sometimes I have to write @objc in front of the method, so that's what @objc and @nonobjc uses in swift.

can someone help me on this issue?

+5
source share
3 answers

This is explained in Apple 's official documentation on Objective-C - Swift compatibility:

When you use the @objc (name) attribute in a Swift class, the class is accessible in Objective-C without any namespace. As a result, this attribute can also be useful when migrating the archived Objective-C class to Swift. Since archived objects store the name of their class in the archive, you must use the @objc (name) attribute to specify the same name as your Objective-C class so that archives can be unpacked by your new Swift class.

Conversely, Swift also provides the @nonobjc attribute, which makes Swift Declaration unavailable in Objective-C. You can use it to allow roundness for bridge methods and allow overloading of methods for classes imported using Objective-C. If the Objective-C method is overridden by the Swift method, which cannot be represented in Objective-C, for example, specifying the parameter as a variable, the method should be marked as @nonobjc.

To summarize, use @objc if you want to set the Swift attribute without a namespace before Objective-C. User @nonobjc, if you want to keep the attribute available and available only for Swift code.

+11
source

(Adding / additional official data to @bontoJR , a good generalizing 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, non-native enumerations (limited to whole types of raw values), properties and methods (including getters and setters) of classes and protocols, initializers, de-initializers and indices. The objc attribute tells the compiler that the declaration is available for use in Objective-Code .

...

nonobjc

Apply this attribute to a method, property, index, or initializer declaration to suppress the implicit objc attribute . nonobjc attribute tells the compiler to make the declaration inaccessible in Objective-C code , although it can be represented in Objective-C.

...

+1
source

Here you can find more information in this Swift Documentation: InteractingWithObjective-C

As an answer to your question, a review from the attached link is given below.

@objc . You can use an attribute to change the name of a class, property, method, type of enumeration, or declaration of an enumeration in your interface, since it is exposed to Objective-C code.

Example : if the name of your Swift class contains a character that is not supported by Objective-C, you can specify an alternative name for use in Objective-C.

@nonobjc : it makes a quick declaration unavailable in Objective-C. You can use it to allow rounding for the method bridge and enable method overloading for classes imported by Objective-C.

0
source

Source: https://habr.com/ru/post/1260979/


All Articles