Xcode7 Beta 4 and @objc problem

I've just installed Xcode 7 beta 4 and suddenly I'm getting a compiler error on


@objc class UtilityFunctions


saying


Only classes that inherit from NSObject can be declared @objc


The 'Using Swift with Cocoa and Object-C (Swift 2 Prerelease)' book states


A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you.


This was working fine in beta 3. I can get it to compile by making the class inheritfrom NSObject but that shouldn't be necessary according to the quote above. Anyone know what's going on? Has @objc suddenly been deprecated in favour of inheriting from NSObject?

Accepted Reply

> Has @objc suddenly been deprecated in favour of inheriting from NSObject?


Pretty much yes. @objc on Swift-rooted classes never quite behaved like an NSObject-rooted class, leading to various weirdness in the generated header and at runtime. You can still treat any Swift class instance as an AnyObject, mark methods and properties on a Swift class as @objc, and conform to Objective-C protocols; the class just isn't exposed in the generated header and doesn't default to having its members available in Objective-C.


(And we should update the book. Thanks for catching that!)

Replies

> Has @objc suddenly been deprecated in favour of inheriting from NSObject?


Pretty much yes. @objc on Swift-rooted classes never quite behaved like an NSObject-rooted class, leading to various weirdness in the generated header and at runtime. You can still treat any Swift class instance as an AnyObject, mark methods and properties on a Swift class as @objc, and conform to Objective-C protocols; the class just isn't exposed in the generated header and doesn't default to having its members available in Objective-C.


(And we should update the book. Thanks for catching that!)

Yeah 7-10-11 is back - Was already missing your posts 🙂

I don't follow any of this! If a class is derived from NSObject, it is completely exposed to Objective-C automatically.


So in that case, what sense does it even make to mark the class as @objc? A rule "you can only mark NSObject-derived classes as @objc" makes almost no sense. The only remaining reason why you might need to do this would be to change the class's Objective-C name.