How to integrate Objective-C and Swift?

Asked

Viewed 780 times

3

I am doing a project on Swift and would like to use some files in Objective-C. It is possible to do this?

In case I installed using the Cocoa Pods, I don’t know if it influences anything.

1 answer

6


Using classes in Objective-C in the Swift

Creating a Bridging Header

When you drag the files to the project

By dragging a .m The Xcode going will most likely show you this window:

Just answer Yes and the Xcode does all the magic for you!

When you use Cocoa Pods

(or when the Xcode doesn’t work its magic)

  • Create a Header of Objetive-C making +N and selected Objective-C Header.
  • Go to the project settings > Build Settings > Swift Compiler - Code Generation and name your Header.

You can do as in this picture: Adding But don’t forget to change the beginning of Path for $(SRCROOT) to ensure that the code compiles on other machines.

Importing classes Objective-C

Just include in your Bridging Header the Imports in the standard Objective-C, for example, to import the class XMLDictionary use

#import "XMLDictionary.h"

Calling methods of Objetive-C in Swift

Now just use the methods, no need to import anything into your classes Swift, it’s that simple!

Patchwork:

  • More information on this subject and source of the images in that question (in English).
  • It is interesting to follow the Xcode name convention and name your bridging header of Meu_Projeto-Bridging-Header.h where your project is called Meu Projeto (trade spaces for _.

For the sake of completeness there goes the answer to the mirror question:

Using class Swift in Objective-C

Use classes Swift in Objective-C is even simpler!

If your class inherits from NSObject

Just import "MeuProjeto-Swift.h" in the Objective-C

If that file doesn’t show up

  • In Build Settings > Packaging:
    • Marque Defines Module as Yes
    • Ensure that Product Module Name possesses only alpha-numeric characters
  • In Bulid Settings > Apple LLVM 6.0 - Language - Modules:
    • Marque Enable Modules (C and Objective-C) as Yes

Important: it is normal that file is not listed, you should be able to import it anyway.

If your class nay inherits from NSObject

  • Prefix @objc before class in your class Swift
  • Create a class method to be your constructor Example:

import Foundation

@objc class ObjetoSwiftPuro {

    var nome: String
    init(nome: String) {
        self.nome = nome
    }

    // Método de classe para retornar instância nova
    class func novaInstanciaNomeada(nome: String) -> ObjetoSwiftPuro {
        return ObjetoSwiftPuro(nome: nome)
    }
}

Full Disclaimer: the part about using Swift in Objective-C was inspired by @Logan with participation of @Tomášlinhart.

  • 1

    Second time I see you post a question and immediately answer. What is the reason?

  • Share knowledge in Q&A style! When I realize that fellow developers come to ask me recurring questions in the post the question and answer in the OS. Take a look here. I do not earn points by marking my own answer as right, I do it to share knowledge, other answers are welcome and if they are better than mine I will not think twice before mark them as "Right answer". :)

  • For those who are downvote, can you explain? This solution is working perfectly for me.

  • A hint is to use $(SRCROOT)/$(PROJECT_NAME)/$(PROJECT_NAME)-Bridging-Header.h when adding to Swift Compiler - Code Generation, so you make sure that Xcode always fetches from the project folder and thus avoiding problems in case there are more than one person working on the project :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.