References to a class just ported to Swift appear as forward class objects

I just ported a new class to Swift to overcome an issue with protocols I had encountered. Yet, after finally fixed all issues associating to the porting, all references to the methods of the class in other objective-c classes are reported as:

"Property ... cannot be found in forward class SwiftClass"

I found a few references to this problem on the web in which it seems the order of inclusion matters, but in all of my classes the *-Header.h inclusion is the last one. What could be the problem and how to fix it?

Answered by fbartolom in 32432022

The error was due to the capitalization of the class name, totally misunderstood by the compiler, and the need to inherit from NSObject in the Swift class. The general lesson is not bieing creating at all in the porting process as the error messaging is misleading, at best.

In fact this time it was right even if the error reporting is sadistic. I had capitalized the class to adopt the Apple suggestion notation expecting the compiler to guide me in the renaming; instead all references to the old name, rather than reporting a normal error consequent to a missing class, reported that funny error taking very far away.


Yet now I get an even funnier error on the alloc operations:

No known class method for selector 'alloc'

... that weird and new component...!

As far as I have experienced, such sort of things happen when whole definition of the SwiftClass is not exported to Objective-C.

Can you see the Objective-C header for the SwiftClass when Command-clicking the *-Header.h ?

Accepted Answer

The error was due to the capitalization of the class name, totally misunderstood by the compiler, and the need to inherit from NSObject in the Swift class. The general lesson is not bieing creating at all in the porting process as the error messaging is misleading, at best.

No known class method for selector 'alloc'

That may happen in some versions of Swift, when @objc put but not inheriting NSObject or its descendent.

Yet, I also found that in a tutorial. Surely if the compiler reported what is wrong rather than composing poetry, that would be very much helpful!

Detecting misspelling issue may be very hard, but the latter, not inheriting NSObject, is checked in the latest Xcode 7. Try it.

References to a class just ported to Swift appear as forward class objects
 
 
Q