Swift protocol in Objective-C class - objective-c

I wrote SearcherProtocol in Swift and need to implement an Objective-C class FileSearcher which has to use this protocol.
So I tried this:
#import <Foundation/Foundation.h>
#interface FileSearcher : NSObject <SearcherProtocol>
// ... class content
#end
The compiler tells me
Cannot find protocol declaration for 'SearcherProtocol'
The corresponding bridged header file (modulename-Swift.h) is being imported within FileSearcher.m.
Importing SearcherProtocol into FileSearcher.h throws another compiler error: module name-swift.h file not found
Does anybody have any clue what I'm doing wrong?
I'm using Xcode 6 Beta 5.
Edit
Here is the protocol declaration in Swift:
#objc protocol SearcherProtocol
{
var searchNotificationTarget: SearchCompletedProtocol? { get }
var lastSearchResults: [AnyObject] { get set }
func search(searchParam: String, error: NSErrorPointer) -> Bool
}
And the SearchCompletedProtocol:
#objc protocol SearchCompletedProtocol
{
func searchCompletedNotification(sender: AnyObject!)
}

There are two common reasons for this occuring:
Getting the module name wrong, see my answer.
Having a circular reference - see mitrenegades answer below.
1. Get the module name right:
If both the swift protocol and and Objective C are in the same project then according to apple you should just need to make sure you get the correct module name.
For Xcode6 beta 5 you can find it under BuildSettings->Packaging->Product Module Name
A common mistake would be to think that each swift file/class gets its own file, but instead they are all put into one big one that is the name of the project.
A further mistakes are if the module name has spaces, these should be replaced with underscores.
Edit:
With your protocol I created a test project called 'Test' which compiles perfectly and it has the files:
TestObjClass.h
#import <Foundation/Foundation.h>
#import "Test-Swift.h"
#interface TestObjCClass : NSObject <SearcherProtocol>
#end
TestObjClass.m
#import "TestObjCClass.h"
#implementation TestObjCClass
#end
TestProtocol.swift
import Foundation
#objc protocol SearcherProtocol
{
var searchNotificationTarget: SearchCompletedProtocol? { get }
var lastSearchResults: [AnyObject] { get set }
func search(searchParam: String, error: NSErrorPointer) -> Bool
}
#objc protocol SearchCompletedProtocol
{
func searchCompletedNotification(sender: AnyObject!)
}
2. Avoid circular reference:
Mitrenegades answer explains this, but if your project needs to use the explicit objc class that uses the swift protocol, (rather than just using the protocol) then you will have circularity issues. The reason is that the swift protocol is defined to the swift-objc header, then to your obj-c class definition, which then goes again to the swift-objc header.
Mitrenegades solution is to use an objective-c protocol, is one way, but if you want a swift protocol, then the other would be to refactor the code so as to not use the objective-c class directly, but instead use the protocol (e.g. some protocol based factory pattern). Either way may be appropriate for your purposes.

When you have
#import "moduleName-Swift.h"
in the .h file that you want to be a delegate, and you have that .h file also in the bridging headers file, there's a circular reference that causes the moduleName-Swift.h to fail compilation. for #james_alvarez's test project, it's probably working because you don't need to include TestObjClass.h into the bridging header.
The best way for me to combine objc files that need to be the delegate for a class written in swift, but that also needs to be included in the bridging header so other swift files can access this objc class, is to create a separate protocol file in objc:
MyProtocol.h:
#protocol MyDelegate <NSObject>
-(void)didDoThis;
-(void)didDoThat;
#end
ViewController.h:
#import "MyProtocol.h"
#interface ViewController : UIViewController <MyDelegate>
MyProject-Bridging-Header.h
#import "MyProtocol.h"
#import "ViewController.h"

I know this was a long time ago, but I just struggled with the same problem when adding a protocol to my Swift code, and it wasn't being added to the -Swift.h header file, hence "Cannot find protocol declaration"
The problem was my protocol wasn't marked as Public. I changed my protocol from this:
#objc protocol MyProtocol { //etc.... }
to this:
#objc public protocol MyProtocol { //etc.... }
I'm still not entirely sure why I need 'Public' but nobody else seems to, but hey it works...

Make sure you are including the auto generated Swift header in your ObjectiveC file. It will have the same name as your project module followed by -Swift.h.
For instance if your Project Module is MyTarget, then you would use:
#import "MyTarget-Swift.h"
If you are typing in the import into your Objective C file, it will not autocomplete. You can verify that you have the correct file by Command-clicking on the header after typing it in.

You can do the conformance part from Swift side 😁
So you have a swift protocol and want to make an Objective-C type conforms to it,
Swift Side
Add #objc to your protocol to make it visible to Objective-C world.
#objc protocol IndianCooking {
func cookChicken()
}
Objective-C Side
In the implementation .m file you do the following:
#import "YourProject-Swift.h"
#interface Cheef ()<IndianCooking> {
}
and in the header file .h add the method
cookChicken()

Import delegate as like this in .h file
#protocol AnalyticProtocol;
and add this in to .swift file
#objc public protocol AnalyticProtocol {
}

Try adding #import "Product_Module_Name-Swift.h" to your Product_Module_Name-Prefix.pch file. That fixed it for me, plus you will now have access to your swift files from any objc file.

Related

How to implement objective C protocol in swift class?

#protocol LibraryPaymentStatusProtocol <NSObject>
#required
-(void)paymentStatus:(NSString*)message;
#optional
-(void)onError:(NSException *)exception;
-(void)tryAgain;
-(void)cancelTransaction;
#end
This protocol I want to implement in my swift class. This protocol is present in BDViewController.h file which is inside the Library. I imported the library successfully in my swift project not getting access to this protocol. What is the additional thing I am missing? Thanks in advance.
In my Swift class, I simply want to implement like normal protocol like below
class mainTabViewController: LibraryPaymentStatusProtocol {
// MARK : - Payment status protocol method
func paymentStatus(_ message: String!) {
}
}
Add this line in the bridging header
#import "BDViewController.h"
maybe it will solve your problem. Let me know if it is helpful.
You need to create a Swift bridging header and include your Objective-C file there.
// <ProjectName>-Bridging-Header.h
#import "BDViewController.h" // In which you have written LibraryPaymentStatusProtocol
Then you can implement the required method as
func paymentStatus(_ message: String?) {
}

iOS11 Swift 4 - how to check if Swift class conforms to protocol defined in Objective-C?

I have a legacy code base with code written in Objective-C. I'm adding a new class written in Swift which has to conform to existing protocols defined in Objective-C.
How can I make sure my Swift class correctly implements methods defined in Objective-C protocol?
//In Obj-C
#protocol OBJCLocationObserver <NSObject>
- (void)didUpdateLocationWithModel:(nullable Model *)locationModel
lastLocation:(CLLocationCoordinate2D)lastLocation;
#end
//In Swift
extension SwiftLocationManager : OBJCLocationObserver
{
public func didUpdateLocation(with model: Model?, lastLocation: CLLocationCoordinate2D) {
// How to verify this function signature is actually conforming to the Obj-C protocol and is not a new method?
}
}
[MyClass conformsToProtocol:#protocol(MyProtocol)];
According to Apple Docs you can use conformsToProtocol:which returns a Boolean value that indicates whether the receiver conforms to a given protocol.
Example
#protocol MyProtocol
- (void)helloWorld;
#end
#interface MyClass : NSObject <MyProtocol>
#end
Will be exposed as:
console.log(MyClass.conformsToProtocol(MyProtocol));
var instance = MyClass.alloc().init();
console.log(instance.conformsToProtocol(MyProtocol))
Make sure you #import your protocol definition file into the <ProjectName>-Bridging-Header.h file:
#import "OBJCLocationObserver.h"
And then you should see error messages if your signature does not match.
You can also use Xcode Auto Completion. Type:
public func didUpdateLocation
and Auto Complete suggests:
public func didUpdateLocation(withModel Model?, lastLocation: CLLocationCoordinate2D)
which is different than what you have and explains why it isn't working.
Here is another way to get the interface:
As #MartinR suggested on a comment to another question:
Go to the header file where the protocol is defined, and choose
"Generated Interface" from the "Related Items" popup in the top-left
corner. That will show you the exact Swift method signature that you
have to implement.

Protocol declared in Objective-C Framework is not visible in Swift

As the title says I have trouble using a protocol declared in Objective-C framework in a Swift app. I will try to describe the situation in detail below.
I have a framework that has mixed Objective-C and Swift code.
This is similar to what I have in the file with protocol definition.
// MyLibrary.h
#protocol MyDelegate <NSObject>;
#interface ClassThatUsesMyDelegate
#property (weak, nonatomic, nullable) id<MyDelegate>delegate;
#end
#protocol MyDelegate
- (void)aMethod;
#end
Objective-C code is wrapped in a module as such:
// module.modulemap
module MyObjCLibrary {
header "MyLibrary.h"
export *
}
Then I have Swift code that uses the Objective-C code via import MyObjCLibrary where all of the declared above in MyLibrary.h is visible and usable.
The umbrella header of the framework has #import "MyLibrary.h". Which should expose all the code from MyLibrary.h to anyone who uses my framework.
Here is where the problem starts. When I embed this framework in my test app I can use any Swift code from my library as well as ClassThatUsesMyDelegate from MyLibrary.h. Howerver, when I try to use the protocol MyDelegate I get a compiler error that this type has not been declared. Example of usage below:
// MyViewController.swift
import UIKit
import MyFramework
class MyViewController: UIViewController {
...
}
extension MyViewController: MyDelegate { // Error: Use of undeclared type 'MyDelegate'
func aMethod() {
...
}
}
EDIT: This seems to be the case only with protocols, everything else like varibles, classes, enums etc are accessible.

objective-c - Swift classes are not visible in Objective-C .h file

I'm trying to use Swift class in my Objective-C file.
I included "moduleName-Swift.h" file in my SuperViewController.m file, but when I try to declare a public method in SuperViewController.h with Swift-class as method parameter, I obviously get an error: "Expected a type"
How can I use swift-class in a header file if I can only include projectModule-Swift.h in .m files??
Make sure to put #objc before the swift class name.
#objc myclassname { ... }
Also add
#class myclassname;
in your obj-c header file where you want to access the swift class
Remember to import the project generated swift header file in your source file, for example #import <MyProjectName-Swift.h>
I had the same problem. Adding #class MyClassName in obj-c .h file created the "Receiver 'MyClassName' for class message is a forward declaration" error, so I deleted that and ONLY added the "#objc" before the swift class name, BUT I also had to make sure that the swift class is a subclass of NSObject.
#objc class MySwiftClass:NSObject { ... }
WITHOUT #class MyClassName in obj-c .h file!
I had to use:
#obj public class MySwiftClass: NSObject { ... }
I created a class with no subclass, adding #objc and #objcmembers did not work.
After I change do :NSObject the entire class turn visible to objective-C

How to make protocol from Swift visible in .h file?

I have a 3 files:
PLThreadViewController.swift
#objc protocol PLThreadViewDelegate {
func threadViewControlledWillDismiss(threadViewController: PLThreadViewController)
}
class PLThreadViewController: UIViewController {}
PLMessagesTableViewController.h
#import <UIKit/UIKit.h>
#interface PLMessagesTableViewController : UITableViewController <PLThreadViewDelegate>
#end
The problem is: Cannot find the protocol declaration for PLThreadViewDelegate
When i try to add:
#import "Module-Swift.h"
then:
Module-Swift.h file not found. The name of module is very correct. I read about spaces, underscores in the name and so on.
Did Xcode create a bridging header for you? If so, look at the naming Xcode uses there and that will clue you in on how your "-Swift.h" file is named. So if your bridging header is named My_Project-Bridging-Header.h, your auto-generated Swift header will be My_Project-Swift.h.