I'm doing a project in Swift and would like to use some files in Objective-C. Is it possible to do this?
In case I installed using Cocoa Pods , I do not know if that influences anything.
I'm doing a project in Swift and would like to use some files in Objective-C. Is it possible to do this?
In case I installed using Cocoa Pods , I do not know if that influences anything.
Dragging a .m
Xcode will will most likely show you this window:
JustanswerYesandXcodedoesallthemagicforyou!
(orwhenXcodedoesnotmagicit)
Youcandoasinthisimage: But be sure to change the start of to to ensure that the code will compile on other machines.
Just add your Bridging Header to imports in the Objective-C pattern, eg to import the%
#import "XMLDictionary.h"
Now just use the methods, you do not have to import anything into your Swift classes, it's as simple as that!
Miscellaneous:
$(SRCROOT)
where your project is called XMLDictionary
(change spaces by Meu_Projeto-Bridging-Header.h
. For completeness purposes there is the answer to the mirror question:
Using Swift classes in Objective-C is even simpler!
Meu Projeto
Just import _
into NSObject
Important : It is normal for this file not to be listed, you should be able to import it anyway.
"MeuProjeto-Swift.h"
Objective-C
prefix before NSObject
in your Swift class
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 use of Swift in Objective-C was inspired by @Logan response with @ TomášLinhart / p>