vikas prajapati
vikas prajapati

Reputation: 1956

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

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

@nonobjc static let test = "test"

and sometimes I have to write @objc before method, so what is use of @objc and @nonobjc in Swift.

Can anyone help me for this problem?

Upvotes: 25

Views: 14595

Answers (4)

Ramdhas
Ramdhas

Reputation: 1765

@objc

When you mark a piece of code with @objc, you are essentially saying that it can be recognized and used by Objective-C code. This is useful when you're working with a mixed codebase that includes both Swift and Objective-C, or when you need to interface with Objective-C frameworks or libraries.

@nonobjc

The @nonobjc attribute is used in Swift to indicate that a certain declaration should not be exposed to Objective-C. It is essentially the opposite of @objc. When you mark a declaration with @nonobjc, you're specifying that it should not be accessible from Objective-C code

Upvotes: 0

bontoJR
bontoJR

Reputation: 7055

This is explained in the Apple's official documentation about Objective-C - Swift interoperability:

When you use the @objc(name) attribute on a Swift class, the class is made available in Objective-C without any namespacing. As a result, this attribute can also be useful when migrating an archivable Objective-C class to Swift. Because archived objects store the name of their class in the archive, you should use the @objc(name) attribute to specify the same name as your Objective-C class so that older archives can be unarchived by your new Swift class.

Conversely, Swift also provides the @nonobjc attribute, which makes a Swift declaration unavailable in Objective-C. You can use it to resolve circularity for bridging methods and to allow overloading of methods for classes imported by Objective-C. If an Objective-C method is overridden by a Swift method that cannot be represented in Objective-C, such as by specifying a parameter to be a variable, that method must be marked @nonobjc.

To summarize, use @objc when you want to expose a Swift attribute to Objective-C without a namespace . Use @nonobjc if you want to keep the attribute available and accessible only in Swift code.

Upvotes: 37

Wolverine
Wolverine

Reputation: 4329

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

As an answer of your question, overview from attached link is as below.

@objc : You can use attribute to change the name of a class, property, method, enumeration type, or enumeration case declaration in your interface as it’s exposed to Objective-C code.

Example : if the name of your Swift class contains a character that isn’t supported by Objective-C, you can provide an alternative name to use in Objective-C.

@nonobjc : It makes a swift declaration unavailable in Objective-C. You can use it to resolve circularity for bridging methods and to allow overloading of methods for classes imported by Objective-C.

Upvotes: 1

dfrib
dfrib

Reputation: 73206

(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.

...

Upvotes: 4

Related Questions