thamil kumaran
thamil kumaran

Reputation: 51

Is it possible to call a AppDelegate method of objective C from swift code of same project

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.

Upvotes: 2

Views: 365

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15778

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())
}

Upvotes: 2

Related Questions