pyanfield
pyanfield

Reputation: 479

Difference between @objc before class and @objc(class_name) above class in swift?

What's the difference between :

@objc class MyClass: NSObject{}

and

@objc(MyClass)
class MyClass: NSObject{}

Upvotes: 9

Views: 1609

Answers (2)

Vatsal
Vatsal

Reputation: 18191

The @objc modifier is being deprecated in Swift 2. All classes that were marked as @objc have to be a subclass of NSObject, thus making the modifier @objc redundant.

@objc(xxx), however, is used to define an alternative name for the class (to be used by the runtime and from Objective-C code).

This modifier is only useful if you want a different name to use in your runtime / Objective C code.

By default the runtime name is same as the declared name, prefixed by the module name and a dot. For example, class X: NSObject {} would be @objc(MyModule.X) at runtime.

Upvotes: 12

rob mayoff
rob mayoff

Reputation: 385920

You can put it on a separate line or not. That is entirely up to you.

If you use @objc(SomeOtherName), it tells the compiler to make the class accessible from Objective-C code using the name SomeOtherName instead of the Swift class name (which might not have an Objective-C equivalent).

Upvotes: 0

Related Questions