11

I'm working on an iOS project that contains Swift and Objective-C Classes.

To instantiate an object described in Objective-C, I've understand that I needed to have a Bridging-Header file that contains all the imports of the headers I will have to use on my Swift classes. And it works great on my project. I made all classes I needed, I subclassed Objective-C classes in Swift, everything is OK.

My problem is when it comes to do the opposite: instantiate a swift object in an Objective-C file. So I read that I need to have those options:

  • Define Modules as Yes
  • add the line #import "<#YourProjectName#>-Swift.h" on the *.m file I'm working on.

And that is what I did:

I changed all the values on the Build Settings that needed to be changed. I added the import line, I even tried to create the header file, but I still have the "<#YourProjectName#>-Swift.h" file not found error.

Of course, I replaced the YourProjectName by the actual module name, the one I found under Product Module Name in Packaging section on Build Settings. Also, there is no spaces in the Project Name.

Did I forgot a step?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Que20
  • 1,469
  • 2
  • 13
  • 27

1 Answers1

8

Make sure you are importing the -Swift.h file from a .m Objective-C file and not a .h header file. The docs only talk about importing from .m:

... import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use your Swift code from.

The -Swift.h file is generated during the build and I've found that importing it from a .h file always fails, even when I know the file exists. Since it's generated by the build, I'm guessing it's cleaned out when the compiler is sifting through the headers and regenerated once the .swift files are compiled.

phatblat
  • 3,804
  • 3
  • 33
  • 31
  • Can I make this work in header files somehow, or circumvent this issue? I need to set a property from Swift on an Objective-C object which is a Swift type — if that still makes sense. – jeroen Dec 15 '15 at 17:54
  • I don't understand what you mean by "Objective-C object which is a Swift type". Swift types cannot be extended in Objective-C. – phatblat Dec 17 '15 at 18:37
  • so @phatblat what you're saying is if I create a class in swift `@objc class Object` I can't use it as a property on an obj-c class header file? – YichenBman Feb 10 '16 at 01:13
  • @phatblat it seems to work for me, unless I then import that objc class in my bridging header so I can use the OBJC object in swift code. – YichenBman Feb 10 '16 at 01:16
  • @YichenBman you can use Swift types in Objective-C, you just can't subclass them https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-NoLink_32 – phatblat Feb 19 '16 at 03:58
  • 1
    @jeroen you can forward declare the Swift class in the .h by declaring it @@class ClassName; – myuiviews Sep 23 '16 at 20:03