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

5

8

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.

Purism answered 6/10, 2017 at 6:42 Comment(1)
@objc weak public var delegate: SwiftLoginVCDelegate?Though
T
9

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

Tchad answered 6/10, 2017 at 8:0 Comment(2)
I have done so , but still getting the same error "No visible @interface for 'SwiftLoginViewController' declares the selector 'setdelegate'; " I am able to access swift class variavle and methods, but I am not able to set delegate declared in swift Class , in objective C class.Purism
Everything is already given to you! I've even provided a sample project in this answer. You need to learn how to use Swift classes in an Objective-C project. Good luck.Tchad
H
2

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.

Harrie answered 2/11, 2017 at 19:39 Comment(0)
S
2

@Gleen response is fine but you must add @objc to var delegate too

weak var delegate: CuteDelegate?
Sumerlin answered 1/2, 2019 at 13:41 Comment(0)
A
0

The issue is coming from using setdelegate instead of setDelegate ( see the lowercase delegate).

Try:

[(SwiftLoginViewController*)self.viewComponent setDelegate:self];
// or 
((SwiftLoginViewController*)self.viewComponent).delegate = self;
Adlare answered 6/10, 2017 at 8:39 Comment(1)
Still getting the error " Property 'delegate' not found on object of type 'SwiftLoginViewController *'""Purism
P
0

I was stuck on this until I found a different problem: I had @objcMembers on the Swift class definition, but the protocol definition sits outside of the class definition and needs its own @objc annotation.

Peruke answered 26/11, 2020 at 1:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.