Can't access Swift class from Objective-C


(Rick M) #1

My Objective-C file is importing "Module-Swift.h", and that file has some of my other Swift classes, but not the one I just wrote. I've tried making the class public, but I didn't need to do that on any of the others.

I've tried cleaning the build folder of my Xcode project, but I get the same result.

Any suggestions? Thanks!

···

--
Rick Mann
rmann@latencyzero.com


(Jordan Rose) #2

Hey, Rick. I think we'd need a bit more information to really diagnose this—in particular, the class declaration in Swift—but here are some possibilities:

- Classes that don't inherit from NSObject are not exposed to Objective-C
- Classes with any generic ancesters are not exposed to Objective-C, even if they themselves are not generic. (There's an implementation reason for this; it's not just the compiler being capricious.)
- In frameworks, only public and open classes are included in the generated header, since it's part of your framework's public interface.

Are you in any of these situations?

Jordan

···

On Apr 17, 2017, at 20:18, Rick Mann via swift-users <swift-users@swift.org> wrote:

My Objective-C file is importing "Module-Swift.h", and that file has some of my other Swift classes, but not the one I just wrote. I've tried making the class public, but I didn't need to do that on any of the others.

I've tried cleaning the build folder of my Xcode project, but I get the same result.

Any suggestions? Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #3

Oh goodness, I'm an idiot. I did not subclass NSObject. BTW, once I've inherited from NSObject, is it still necessary to apply @objc?

And related, the ModuleName-Swift.h header that's generated isn't automatically including the C header that's used by my Swift code (my Swift code wraps a C API that I publish using a modulemap file). Now, I haven't actually included the .map file anywhere in the Xcode project, I just have an import search path pointing to the directory that contains it, the header, and the .dylib, and a library search path pointing to that same directory.

The Swift code is able to import the header and link against the library (AFAICT). I can certainly include the header in my PCH, but I would have hoped the generated -Swift.h would be smart enough to pick up the header file based on the .map file, but maybe it's supposed to be added to the Xcode project?

My module.map file looks like this:

module Colibri [extern_c] {
    header "Colibri.h"
    export *
}

···

On Apr 18, 2017, at 10:45 , Jordan Rose <jordan_rose@apple.com> wrote:

Hey, Rick. I think we'd need a bit more information to really diagnose this—in particular, the class declaration in Swift—but here are some possibilities:

- Classes that don't inherit from NSObject are not exposed to Objective-C
- Classes with any generic ancesters are not exposed to Objective-C, even if they themselves are not generic. (There's an implementation reason for this; it's not just the compiler being capricious.)
- In frameworks, only public and open classes are included in the generated header, since it's part of your framework's public interface.

Are you in any of these situations?

Jordan

On Apr 17, 2017, at 20:18, Rick Mann via swift-users <swift-users@swift.org> wrote:

My Objective-C file is importing "Module-Swift.h", and that file has some of my other Swift classes, but not the one I just wrote. I've tried making the class public, but I didn't need to do that on any of the others.

I've tried cleaning the build folder of my Xcode project, but I get the same result.

Any suggestions? Thanks!

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com