Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use Swift classes inside Objective-C

I try to integrate Swift code in my app.My app is written in Objective-C and I added a Swift class. I've done everything described here. But my problem is that Xcode haven't created the -Swift.h file, only the bridging headers. So I created it, but it's actually empty. I can use all my ObjC classes in Swift, but I can't do it vice versa. I marked my swift class with @objc but it didn't help. What can I do now?

EDIT: Apple says:" When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. [...] The name of this header is your product module name followed by adding “-Swift.h”. "

Now when I want to import that File, it gives an error:

    //MainMenu.m      #import "myProjectModule-Swift.h" //Error: 'myProjectModule-Swift.h' file not found      @implementation MainMenu 

Here is my FBManager.swift file:

@objc class FBManager: NSObject {      var descr = "FBManager class"      init() {         super.init()     }      func desc(){         println(descr)     }      func getSharedGameState() -> GameState{         return GameState.sharedGameState() //OK! GameState is written in Objective-C and no error here     } } 
like image 609
dnnagy Avatar asked Jun 13 '14 13:06

dnnagy


People also ask

Can we use Swift class in Objective-C?

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code.

Can I use Swift library in Objective-C?

The Swift library cannot be directly called from Objective-C, since it is missing the required annotations in the code, and in many cases, modules do not inherit from NSObject, rather they use the native Swift data types.

How do you subclass a Swift class in Objective-C?

Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs: You cannot subclass a Swift class in Objective-C. See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.


2 Answers

I spent about 4 hours trying to enable Swift in my Xcode Objective-C based project. My myproject-Swift.h file was created successfully, but my Xcode didn't see my Swift-classes. So, I decided to create a new Xcode Objc-based project and finally, I found the right answer! Hope this post will help someone :-)

Step by step Swift integration for Xcode Objc-based project:

  1. Create new *.swift file (in Xcode) or add it by using Finder.
  2. Create an Objective-C bridging header when Xcode asks you about that.
  3. Implement your Swift class:

    import Foundation  // use @objc or @objcMembers annotation if necessary class Foo {     //.. } 
  4. Open Build Settings and check these parameters:

    • Defines Module : YES

      Copy & Paste parameter name in a search bar

    • Product Module Name : myproject

      Make sure that your Product Module Name doesn't contain any special characters

    • Install Objective-C Compatibility Header : YES

      Once you've added *.swift file to the project this property will appear in Build Settings

    • Objective-C Generated Interface Header : myproject-Swift.h

      This header is auto-generated by Xcode

    • Objective-C Bridging Header : $(SRCROOT)/myproject-Bridging-Header.h
  5. Import Swift interface header in your *.m file.

    #import "myproject-Swift.h" 

    Don't pay attention to errors and warnings.

  6. Clean and rebuild your Xcode project.
  7. Profit!
like image 120
sig Avatar answered Oct 06 '22 02:10

sig


Don't create the header file yourself. Delete the one you created.

Make sure your Swift classes are tagged with @objc or inherit from a class that derives (directly or indirectly) from NSObject.

Xcode won't generate the file if you have any compiler errors in your project - make sure your project builds cleanly.

like image 23
Bill Avatar answered Oct 06 '22 01:10

Bill