6

My Project is in Swift 3.0 and I am using xCode8.2. I need to import objective C code in my swift project. objective C required app delegate reference. How can I get swift appdelegate reference from my objective C code?

In my objective C file #import "AppDelegate.h" is giving not found error as it is swift AppDelegate

3
  • Are you using the objective c code form your swift code? Is it initalized in swift with an app delegate? Commented Feb 28, 2017 at 11:33
  • MY project is in Swift , I have a module which is build in Objective C. in that objective c module it require appdelegate reference. Commented Feb 28, 2017 at 11:46
  • I need it in reverse. From Objective c code I need a reference of swift appdelegate. #import "AppDelegate.h" is giving not found error as it is swift AppDelegate Commented Feb 28, 2017 at 12:11

3 Answers 3

8

As Kie already said, you need to #import "<ProductModuleName>-Swift.h" in your Obj-C .m file to get all Swift classes. Here a sample project with what you need https://github.com/aminbenarieb/Researches/tree/master/Swift/Swift_ObjC.

Update: If your can't see Swift files in Objective-C files, make sure you have the Objective-C bridging header, as follows in Apple Documentation:

Importing Objective-C into Swift

To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

Sign up to request clarification or add additional context in comments.

2 Comments

I did the similar this like your Foo.m , I added #import "MyProjectName-Swift.h in the ObjC.m file . But It says not able to find MyProjectName-Swift.h file.
@AvijitDas, 1. To download the code make sure that git installed on you machine and - run in terminal: git clone https://github.com/aminbenarieb/Researches.git Researches cd Researches git filter-branch --prune-empty --subdirectory-filter Swift/Swift_ObjC HEAD - otherwise you manually download project and find SwiftObjC project folder. Have you added the Objective-C bridging header ?
8

I am not sure if this answers your question.

Getting AppDelegate in Swift

You can call objective-c code from Swift and if your Objective-C code needs an AppDelegate for it to work in some calls you can gat this by calling UIApplication.shared.delegate in Swift.

let appDelegate = UIApplication.shared.delegate as! AppDelegate

Getting AppDelegate in Objective-C

If you need the AppDelegate in your Objective-C code thats the way:

#import "<ProductModuleName>-Swift.h" // You have to replace with your swift module name
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

8 Comments

I need it in reverse. From Objective c code I need a reference of swift appdelegate. #import "AppDelegate.h" is giving not found error as it is swift AppDelegate
I edited my answer. To really help you, it'll help if you could post some of your code next time. Thank you.
I guess it is because you didn't figure out how to import your swift header. I added a line, but this will only work if your Objective-C code is compiled in the same target. More information on that here: developer.apple.com/library/content/documentation/Swift/… Btw: I don't think that's a nice down-vote since I am making an effort to help you, despite that the information you are giving is lacking.
Sorry for the down vote as I thought you are not getting my question and you are answering in reverses way. I just saw your comment and undo it. developer.apple.com/library/content/documentation/Swift/… This link I saw. I gave "#import "MyProjectBundleName-Swift.h" in the objective C class. But when I am trying to compile its not finding the header file.
Hi , It Worked , Thanks. I need to make few settings in my Build settings from the instruction in stackoverflow.com/questions/24102104/…
|
0

Adding some color to the answers from Kie and Amin: I was struggling with how to adapt #import "<ProductModuleName>-Swift.h" into something that made sense for my project. I was getting header file not found compile-time error, and it turned out that non-alphanumeric characters in ProductModuleName are replaced with underscores when the bridge header is created. In my case I had a space in the product name; once I replaced the space with an underscore the compile-time error was resolved. Seems trivial now, but I'm just starting out.

Here's the reference to Apple's documentation and relevant snippet:

The header's name is generated from your product module name, followed by "-Swift.h". By default, this name is the same as your product name, with any non-alphanumeric characters replaced with an underscore (_). If the name begins with a number, the first digit is replaced with an underscore.

Importing Swift into Objective-C

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.