I need to call my AppDelegate
function of Objective-C from the Swift class of the same project.
The function definition is in Objective-C AppDelegate
.
I need to call from Swift class existing in the same project.
Asked
Active
Viewed 323 times
2

rmaddy
- 314,917
- 42
- 532
- 579

thamil kumaran
- 51
- 3
-
2Possible duplicate of [How to call Objective-C code from Swift](https://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift) – Hitesh Surani Apr 29 '19 at 07:06
-
@iMHiteshSurani - read the question carefully, from appdelegate not from objective C file – Anbu.Karthik Apr 29 '19 at 07:08
-
2@Anbu.Karthik Appdelegate file was written in objective C as mention in question. – Hitesh Surani Apr 29 '19 at 07:13
1 Answers
2
Create a Bridging Header file and add
#import "AppDelegate.h"
Add your function definition in AppDelegate.m and function declaration
AppDelegate.m
- (NSString *)test {
return @"test";
}
AppDelegate.h
- (NSString *)test;
In Swift file
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
print(appDelegate.test())
}

RajeshKumar R
- 15,445
- 2
- 38
- 70
-
if i import AppDelegate.h in bridgeheader i'm getting error that some of your pod file is missing. – thamil kumaran Apr 29 '19 at 10:43
-
-
No need to add #import "'AppDelegate.h" in the Bridging Header File, simply use let myAppdelegateObj = UIApplication.shared.delegate as! AppDelegate & myAppdelegateObj.yourFunctionName() . – Friend Apr 29 '19 at 11:09
-
-
-
@Friend Have you tried it by running? How could the swift file know there is a class named AppDelegate without importing it in bridging header? – RajeshKumar R Apr 29 '19 at 11:17
-
-
Then you would've imported other objective c files which imports appdeletgate – RajeshKumar R Apr 30 '19 at 05:38