Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conform swift class delegate in objective c Class using swift 4?

Suppose there are two class one in swift and other is in objective-c class in same project.

In swift class i declared delegate and i want to set that delegate in objective c class.

I have done this following way ...

import UIKit

@objc public protocol SwiftLoginVCDelegate: class {

}

@objc public class SwiftLoginViewController: UIViewController {

    @IBOutlet var txtUserName: UITextField!
    @IBOutlet var txtPassword: UITextField!
    @IBOutlet var btnLogin: UIButton!
    var delegate: SwiftLoginVCDelegate?

    override public func viewDidLoad() {
        super.viewDidLoad()
        self.txtPassword.text = "XYZ"
        self.txtPassword.text = "123"
        self.btnLogin.backgroundColor = UIColor.blue

    }
 @objc public func testObjective(){
        print("Swift class is integrated in objective c project")
    }

the objective c class is

#import "Mediator.h"

@class    SwiftLoginViewController;
@protocol SwiftLoginVCDelegate;

@interface LoginMediator : Mediator

@end

Implementation Class is

#import "xyz-Swift.h"

@class SwiftLoginViewController;

@implementation LoginMediator 


-(void)onRegister
{
// line 1:   [(XYZController*)self.viewComponent setDelegate:self];

   line 2 :  [(SwiftLoginViewController*)self.viewComponent setDelegate:self];

       [objectVC testObjective];
}

If u check the onRegister Method , In line No 1 delegate is set using objective c, which is commented now , but i want to set same delegate in swift 4 , line no 2, when I try to set delegate in swift 4 I am getting following error

No visible @interface for 'SwiftLoginViewController' declares the selector 'setdelegate';

Note :

One more that i am able to access all the var and function defined in swift class to objective c Class. I am not able to set the Delegate in objective c Class which is declared in swift Class.

Can any one has any idea what i am doing wrong in above code ? All inputs are appreciated.

like image 638
Shubham JAin Avatar asked Jan 29 '23 11:01

Shubham JAin


2 Answers

Okay so I've made a sample project in Objective-C and then installed my Swift framework called GPKit, once I made it working, I realized you're not using Cocoapod. So I made a sample Swift class and then used it in my Objective-C class.

First, you need to learn how to properly use a Swift file/class inside your Objective-C class, learn from here: Can't use Swift classes inside Objective-C

And then once you get it working, confirming to a Swift delegate and implementing the functions in that delegate will be easy.

What I can see in your implementation is that you're making a new class and protocol.

@class    SwiftLoginViewController;
@protocol SwiftLoginVCDelegate;

Here's the sample code that I made just for this question:

ViewController.m

#import "TestObjcUsingGPKit-Swift.h"
#import "ViewController.h"

@interface ViewController () <CuteDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CuteClass *newCutie = [[CuteClass alloc] init];
    newCutie.delegate = self;
}

- (void)myCuteFunc {
    // --- the delegate function
}

@end

CuteClass.swift

import UIKit

@objc public protocol CuteDelegate: NSObjectProtocol {
    @objc func myCuteFunc()
}

public class CuteClass: NSObject {
    weak var delegate: CuteDelegate?
}

The full sample project on GitHub for you: https://github.com/glennposadas/UsingSwift-In-ObjectiveC

like image 76
Glenn Posadas Avatar answered Feb 04 '23 03:02

Glenn Posadas


I had the exact same issue as you. I noticed that the generated -Swift.h file did not expose the weak delegate member in my Swift file to the objective-c runtime. You can verify this by doing a command+click on your #import -Swift.h file and search for you Swift class name to see what methods/members are exposed.

To finally fix the issue, I ended up adding the @objc tag to my delegate. In your case it would be:

@objc weak var delegate: SwiftLoginVCDelegate?

After I did that, I observed the -Swift.h file again, and lo and behold my delegate was exposed and I'm able to use it in my objective c file as expected.

Hope this helps.

like image 22
benzo Avatar answered Feb 04 '23 03:02

benzo