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.
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.
6
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!
(or when the Xcode doesn’t work its magic)
You can do as in this picture:
But don’t forget to change the beginning of Path for $(SRCROOT)
to ensure that the code compiles on other machines.
Just include in your Bridging Header the Imports in the standard Objective-C, for example, to import the class XMLDictionary
use
#import "XMLDictionary.h"
Now just use the methods, no need to import anything into your classes Swift, it’s that simple!
Patchwork:
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:
Use classes Swift in Objective-C is even simpler!
NSObject
Just import "MeuProjeto-Swift.h"
in the Objective-C
Important: it is normal that file is not listed, you should be able to import it anyway.
NSObject
@objc
before class
in your class Swiftimport 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.
Browser other questions tagged ios objective-c swift
You are not signed in. Login or sign up in order to post.
Second time I see you post a question and immediately answer. What is the reason?
– André Ribeiro
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". :)
– fpg1503
For those who are downvote, can you explain? This solution is working perfectly for me.
– fpg1503
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 :)– Nathan Hegedus