简体   繁体   English

Swift中不可见的Objective-C协议方法

[英]Objective-C protocol method invisible in Swift

I'm currently working on some Swift classes in my ObjC project. 我目前正在ObjC项目中处理一些Swift类。

The problem I have is the following: 我的问题如下:

I have this protocol declared in ClassA.h: 我在ClassA.h中声明了以下协议:

@protocol MyProtocol <NSObject>
    - (void)complexMethodWithArg1:(id)arg1 arg2:(id)arg2 arg3:(id)arg3;
    - (Folder *)currentDestinationFolder;
    - (Flow)currentFlow;
@end

Pretty standard stuff. 很标准的东西。

Now my goal is to have a swift class with a property that is an object implementing this protocol. 现在,我的目标是创建一个带有属性的swift类,该属性是实现此协议的对象。 So naturally, I add my class to the swift bridging header: 很自然地,我将我的类添加到快速桥接头文件中:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "ClassA.h"

and declare my property in my swift file under ClassB which is a UIViewController that implement ANOTHER protocol 并在ClassB下的swift文件中声明我的属性,该文件是实现ANOTHER协议的UIViewController

class ClassB : UIViewController, AnotherProtocol {

    var delegate:MyProtocol?

}

Problem here is: I want to call a bunch of my delegate methods in viewDidLoad . 这里的问题是:我想在viewDidLoad调用一堆我的委托方法。 It's working for all of them except ONE method that gets not autocompletion and errors the compilation if entered manually: 它适用于所有其他方法,只有一种方法无法自动完成,并且如果手动输入会导致编译错误:

override func viewDidLoad() {
    self.delegate?.currentDestinationFolder() // works great, no problem
    self.delegate?.currentFlow() // works great, no problem
    self.delegate?.complexMethodWithArg1(arg1: arg1, arg2: arg2, arg3: arg3) // PROBLEM : no autocompletion, error if entered manually ! 

    super.viewDidLoad()
}

I have no idea what's going on, it's not related to optional or required protocol methods, not related to the fact that my delegate property is optional (tried unwrapped). 我不知道发生了什么,它与可选的或必需的协议方法无关,与我的委托属性是可选的(未包装)有关。

Has anybody face some similar issue? 有人遇到过类似的问题吗? seems like some kind of bug? 似乎是某种错误?

I went ahead and tried to reproduce the problem on an empty project. 我继续尝试在一个空项目中重现该问题。

MyProtocol.h (taking the declaration from your question and comments) MyProtocol.h (从您的问题和评论中获取声明)

@import Foundation;
@import UIKit;

@class CAPNavigationBar;

@protocol MyProtocol <NSObject>

- (void)setupNavigationItemInNavigationBar:(CAPNavigationBar *)navigationBar
                            navigationItem:(UINavigationItem *)navigationItem
                          inViewController:(UIViewController *)viewController;

@end

CAPNavigationBar.h (just a mock) CAPNavigationBar.h (只是一个模拟)

@import Foundation;

@interface CAPNavigationBar : NSObject

@end

ViewController.swift ViewController.swift

import UIKit

class ViewController: UIViewController {
    var delegate: MyProtocol?

    override func viewDidLoad() {
        super.viewDidLoad()

        let capNavigationBar = CAPNavigationBar()

        self.delegate?.setupNavigationItemInNavigationBar(capNavigationBar, navigationItem: nil, inViewController: self)
    }
}

Bridging header 桥接头

#import "MyProtocol.h"
#import "CAPNavigationBar.h"

Summary 摘要

Everything is working as expected. 一切都按预期进行。

You have either a simple typo somewhere or you are not importing correctly all the types into Swift. 您可能在某个地方有一个简单的错字,或者没有将所有类型正确地导入到Swift中。 Especially make sure that you are not importing types only as forward declarations. 尤其要确保不要将类型仅作为前向声明导入。

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

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