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

Avijit Das
  • 300
  • 1
  • 5
  • 20
  • Are you using the objective c code form your swift code? Is it initalized in swift with an app delegate? – kiecodes Feb 28 '17 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. – Avijit Das Feb 28 '17 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 – Avijit Das Feb 28 '17 at 12:11

3 Answers3

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];
kiecodes
  • 1,642
  • 14
  • 28
  • 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 – Avijit Das Feb 28 '17 at 12:10
  • I edited my answer. To really help you, it'll help if you could post some of your code next time. Thank you. – kiecodes Feb 28 '17 at 12:17
  • 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: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html 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. – kiecodes Feb 28 '17 at 13:12
  • 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. https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html 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. – Avijit Das Mar 01 '17 at 06:31
  • What is your Bundle name? – kiecodes Mar 01 '17 at 07:09
  • Additionally is to add that if you are trying to use a framework you have to do additionally steps. Let me know if that is the case. – kiecodes Mar 01 '17 at 07:12
  • 1
    Hi , It Worked , Thanks. I need to make few settings in my Build settings from the instruction in http://stackoverflow.com/questions/24102104/how-to-import-swift-code-to-objective-c?rq=1 – Avijit Das Mar 02 '17 at 09:56
  • Should I need to add `#import "AppDelegate.swift"` file in `ProjectName-Swift.h` file ? – Vivek Jun 03 '19 at 05:44
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.

Amin Benarieb
  • 356
  • 4
  • 7
  • 1
    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. – Avijit Das Mar 01 '17 at 06:40
  • @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](https://github.com/aminbenarieb/Researches/archive/master.zip) and find SwiftObjC project folder. Have you added the `Objective-C bridging header `? – Amin Benarieb Mar 01 '17 at 09:18
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

sherb
  • 5,845
  • 5
  • 35
  • 45