0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Champoul
  • 1,004
  • 7
  • 15
  • self.delegate?.complexMethodWithArg1 is not completely declare – baydi Oct 27 '15 at 10:08
  • @baydi yeah I know, it's only to show that there is no autocompletion. It fails completely declared as well. – Champoul Oct 27 '15 at 10:10
  • May be it should be: `- (void)complexMethodWithArg1:(id)arg1 arg2:(id)arg2 arg3:(id)arg3;`? – Cy-4AH Oct 27 '15 at 10:12
  • yeah, thanks for pointing this out, just a typo, I'm not allowed to display the actual code. – Champoul Oct 27 '15 at 10:13
  • Your problem probably lies it the actual code. You you please create a minimal example that reproduces the problem? For now it's impossible for us to help you. What error you get when the method name is entered manually? – Sulthan Oct 27 '15 at 10:14
  • 1
    Try `self.delegate?.complexMethodWithArg1(arg1, arg2: arg2, arg3: arg3)` or `self.delegate?.complexMethodWith(arg1: arg1, arg2: arg2, arg3: arg3)` – Cy-4AH Oct 27 '15 at 10:15
  • @Sulthan I only changed method naming and variable names, otherwise the code is exactly as provided. – Champoul Oct 27 '15 at 10:15
  • @Champoul The method name can be a problem. Is the method name an identifier in Swift? – Sulthan Oct 27 '15 at 10:16
  • @Sulthan yeah I thought about that so played around with the name, but no luck, here's the actual name : `- (void)setupNavigationItemInNavigationBar:(CAPNavigationBar *)navigationBar navigationItem:(UINavigationItem *)navigationItem inViewController:(UIViewController *)viewController;` – Champoul Oct 27 '15 at 10:17

1 Answers1

2

I went ahead and tried to reproduce the problem on an empty project.

MyProtocol.h (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

CAPNavigationBar.h (just a mock)

@import Foundation;

@interface CAPNavigationBar : NSObject

@end

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. Especially make sure that you are not importing types only as forward declarations.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • 1
    Damn, that's it ! I did not had the `#import "CAPNavigationBar.h"` in my bridge. I guess the bridge could not create the method as swift was not aware of one of the argument types in the protocol methods. Thanks a lot ! – Champoul Oct 27 '15 at 10:41