1

This question is basically the same as this one, but that one went unanswered.

TL;DR: Protocol does not show up in MyProject-Swift.h secret bridging file. And so (not unexpectedly) the Obj-C code can't find it.

I have been able to duplicate it in a from-scratch project. Here is what I did.

  1. Create new one-view iOS Objective-C project.
  2. Add a new Swift UIView subclass via "New File" menu.
  3. Confirm that MyProject-Bridging-Header.h was created. (Visibly)
  4. Confirm that the secret MyProject-Swift.h file was automatically created. (Way down there in DerivedData/.../DerivedSources).
  5. Add an instance of this View to my ViewController.m.
  6. Include the MyProject-Swift.h file in ViewController.m.
  7. Successfully build -- sanity check

Now for the weirdness...

  1. Add a Swift protocol file via "New File" menu.
  2. Make ViewController conform to and implement this protocol.
  3. Build fails at this spot in ViewController.m:

    #import "MyProject-Swift.h" @interface ViewController () <MyProtocol> // Can't find protocol declaration

Examining the MyProject-Swift.h file confirms that the protocol is not in there. But poking around some big pre-existing hybrid projects (that do build) I don't see any protocols in any of their -Swift.h files.

How do Swift protocols get recognized in Objective-C?

Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55
  • Following your instruction 1...10, my sample project compiles fine and `MyProtocol` can be found in `MyProject-Swift.h`. Is the protocol really Objective-C compatible? (Having `@objc`?) Have you successfully compiled the protocol's `.swift` file before using it (between your #8 and #9) ? – OOPer Dec 02 '17 at 03:31
  • Ah `@objc` that did it. I guess in the pre-existing hybrid projects I looked at, the Swift protocols were only used by Swift classes. On a side note, when I launched Xcode this AM and tried to build (before adding `@objc`) I got the *additional* error of` `-Swift.h` file not found. Even though it did exist. I had this happening sporadically during my debugging of this issue, and it hampered my search a lot. Bug? Anyway, write your suggestion up and I'll mark it as the answer. – Andrew Duncan Dec 02 '17 at 17:38
  • If `@objc` is the only issue, there are some threads describing it, for example, [this one](https://stackoverflow.com/q/24078242/6541007). And about errors of _`-Swift.h` file not found_, some sort of errors prevent refreshing `-Swift.h`. Comment out all errors as to build successfully, and then uncomment them one by one. – OOPer Dec 03 '17 at 00:48
  • You are right, I wish I'd found the thread you mentioned. Regarding the file not found, it is not practical to comment out all code that needs it, since I am porting a 700+ file project from Obj-C to Swift. However, doing a Clean Project *and* quitting and restarting Xcode seems to fix things. – Andrew Duncan Dec 03 '17 at 01:03

1 Answers1

3

I have done this for my project

while defining protocol you can add @objc before the protocol definition

@objc protocol NewViewControllerDelegate {

}

And you are done

Note : I think @objc makes swift protocols and classes visible to objective c

Hitesh Sultaniya
  • 929
  • 1
  • 7
  • 12