3

I've defined a protocol in a Swift file that isn't being recognized by an Obj-C header file.

MainViewController.h

#import "AppDelegate.h"

@interface MainViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, AnotherViewControllerDelegate>
@end

In th file above, I'm getting the following error:

Cannot find protocol declaration for "AnotherViewControllerDelegate"; did you mean "UIPageViewControllerDelegate"?

MainViewController.m

#import "MainViewController.h"
#import "AppDelegate.h"

@implementation MainViewController

-void closeView{
     NSLog(@"Why doesn't this work?");
}

@end

AppDelegate.h

#import "MyProject-Swift-Headers.h"

AnotherViewController.swift

@objc protocol AnotherViewControllerDelegate {
    func closeView()
}

@objc class AnotherViewController: UIViewController{

     var delegate: AnotherViewControllerDelegate!

     @IBAction func closeButton(sender: UIButton) {
          delegate.closeView()
     }
}

What's strange is that in MainViewController.h, Xcode will autocomplete when I start typing "AnotherViewControllerDelegate". Additionally, "Defines Modules" under Build Settings -> Packaging has always been set to "Yes"; I've mixed Swift and Obj-C methods for other parts of this project's codebase... just can't seem to get this protocol to work.

Vee
  • 1,821
  • 3
  • 36
  • 60
  • It is `Switft.h` not `Swift-Headers.h` also try to import in `.m` file. – Nirav D Sep 08 '16 at 04:57
  • Can you explain the first part of your statement? Also, I tried importing "MyProject-Swift-Headers.h" in both the .m and .h files of MainViewController... got the same error. – Vee Sep 08 '16 at 05:46
  • It is `MyProject-Swift.h` not `MyProject-Swift-Headers.h`. Check this apple doc https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html and read for `Importing Swift into Objective-C` – Nirav D Sep 08 '16 at 05:49
  • I don't think that's the problem bc I've been importing the "#import "MyProject-Swift-Headers.h" header file into other Obj-C files and have successfully used other Swift methods. Plus, I just tried copying the contents of "MyProject-Swift-Headers.h" into a newly created "MyProject-Swift.h" file and the error didn't go away. – Vee Sep 08 '16 at 06:12
  • Is the "Cannot find protocol declaration..." message an error, or just a warning? I've seen such warnings that turn out to have no effect on functionality. Are you using a bridging header? What is the value of "Objective-C Generated Interface Header Name" property under Build Settings? – Anatoli P Sep 13 '16 at 02:26

0 Answers0