简体   繁体   English

如何使用swift 4在目标c类中符合swift类委托?

[英]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.假设有两个类,一个在 swift 中,另一个在同一个项目中的objective-c 类中。

In swift class i declared delegate and i want to set that delegate in objective c class.在 swift 类中,我声明了委托,我想在目标 c 类中设置该委托。

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目标 c 类是

#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如果您检查 onRegister 方法,则使用目标 c 设置第 1 行委托,现在已对其进行注释,但我想在 swift 4 第 2 行中设置相同的委托,当我尝试在 swift 4 中设置委托时,我得到了跟随错误

No visible @interface for 'SwiftLoginViewController' declares the selector 'setdelegate'; “SwiftLoginViewController”没有可见的@interface 声明了选择器“setdelegate”;

Note :注意:

One more that i am able to access all the var and function defined in swift class to objective c Class.另一个我能够访问 swift 类中定义的所有 var 和函数到目标 c 类。 I am not able to set the Delegate in objective c Class which is declared in swift Class.我无法在 swift 类中声明的目标 c 类中设置委托。

Can any one has any idea what i am doing wrong in above code ?任何人都知道我在上面的代码中做错了什么吗? All inputs are appreciated.感谢所有输入。

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 .好的,所以我在 Objective-C 中创建了一个示例项目,然后安装了我的 Swift 框架GPKit ,一旦我让它工作,我意识到你没有使用Cocoapod So I made a sample Swift class and then used it in my Objective-C class.所以我制作了一个示例 Swift 类,然后在我的 Objective-C 类中使用它。

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首先,您需要学习如何在 Objective-C 类中正确使用 Swift 文件/类,从这里学习: 不能在 Objective-C 中使用 Swift 类

And then once you get it working, confirming to a Swift delegate and implementing the functions in that delegate will be easy.然后一旦你让它工作,确认一个 Swift 委托并在该委托中实现功能将很容易。

What I can see in your implementation is that you're making a new class and protocol .我在您的实现中看到的是您正在创建一个新的classprotocol

@class    SwiftLoginViewController;
@protocol SwiftLoginVCDelegate;

Here's the sample code that I made just for this question:这是我为这个问题制作的示例代码:

ViewController.m视图控制器.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可爱类.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 GitHub 上为您提供的完整示例项目: https : //github.com/glennposadas/UsingSwift-In-ObjectiveC

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.我注意到生成的 -Swift.h 文件没有将我的 Swift 文件中的弱委托成员暴露给 Objective-c 运行时。 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.您可以通过在 #import -Swift.h 文件上执行命令+单击并搜索您的 Swift 类名来查看暴露了哪些方法/成员来验证这一点。

To finally fix the issue, I ended up adding the @objc tag to my delegate.为了最终解决这个问题,我最终将 @objc 标签添加到我的委托中。 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.在我这样做之后,我再次观察了 -Swift.h 文件,瞧,我的委托被暴露了,我可以按预期在我的目标 c 文件中使用它。

Hope this helps.希望这会有所帮助。

@Gleen 响应很好,但您也必须将 @objc 添加到 var 委托

weak var delegate: CuteDelegate?

The issue is coming from using setdelegate instead of setDelegate ( see the lowercase delegate).问题来自使用setdelegate而不是setDelegate (请参阅小写委托)。

Try:尝试:

[(SwiftLoginViewController*)self.viewComponent setDelegate:self];
// or 
((SwiftLoginViewController*)self.viewComponent).delegate = self;

我一直坚持这个,直到我发现一个不同的问题:我在 Swift 类定义上有@objcMembers ,但协议定义位于类定义之外,需要自己的@objc注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM