简体   繁体   中英

Objective-C protocol method invisible in Swift

I'm currently working on some Swift classes in my ObjC project.

The problem I have is the following:

I have this protocol declared in 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. 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

class ClassB : UIViewController, AnotherProtocol {

    var delegate:MyProtocol?

}

Problem here is: I want to call a bunch of my delegate methods in 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.

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

@import Foundation;
@import UIKit;

@class CAPNavigationBar;

@protocol MyProtocol <NSObject>

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

@end

(just a mock) (只是一个模拟)

@import Foundation;

@interface CAPNavigationBar : NSObject

@end

import UIKit

class ViewController: UIViewController {
    var delegate: MyProtocol?

    override func viewDidLoad() {
        super.viewDidLoad()

        let capNavigationBar = CAPNavigationBar()

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

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

Everything is working as expected.

You have either a simple typo somewhere or you are not importing correctly all the types into Swift. Especially make sure that you are not importing types only as forward declarations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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