Gal
Gal

Reputation: 1582

Swift protocol error

I declared protocol in my Swift file:

protocol SocketManagerDelegate {
   func webSocketDidReceiveMessage(message:Message)
   func socketWasReconnected()
   func webSocketDidFailWithError(error:String)
}

import Foundation
import MDWamp
import SSKeychain

@objc(SocketManager)
class SocketManager: NSObject, MDWampClientDelegate{

   static let instance = SocketManager()

   var delegate:SocketManagerDelegate?

and I want to use the protocol in an ObjC file:

#import <Project-Swift.h>

@interface ChatManager () <SocketManagerDelegate>
@end

@implementation ChatManager.........

I'm getting this error:

enter image description here

Can't figure this out. Ideas?

In my project-swift.h file the variant delegate is not showing at all:

SWIFT_CLASS_NAMED("SocketManager")
@interface SocketManager : NSObject <MDWampClientDelegate>
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, strong) 
SocketManager * _Nonnull instance;)
+ (SocketManager * _Nonnull)instance SWIFT_WARN_UNUSED_RESULT;
@property (nonatomic, strong) MDWamp * _Null_unspecified wampConnection;
@property (nonatomic, readonly, copy) NSString * _Nonnull socketUrlSecure;
@property (nonatomic, readonly, copy) NSString * _Nonnull socketUrl;

Upvotes: 1

Views: 816

Answers (3)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16426

You are using swift protocol

protocol SocketManagerDelegate {
   func webSocketDidReceiveMessage(message:Message)
   func socketWasReconnected()
   func webSocketDidFailWithError(error:String)
}

that can't be used in obj-c without

@Objc statement

please replace your code with

@objc protocol SocketManagerDelegate:class {
   func webSocketDidReceiveMessage(message:Message)
   func socketWasReconnected()
   func webSocketDidFailWithError(error:String)
}

Upvotes: 2

Yerken
Yerken

Reputation: 297

Your protocol doesn't confirm objC. Protocols in swift and in objC are different

add clas to your protocol:

@objc protocol SocketManagerDelegate: class {
   func webSocketDidReceiveMessage(message:Message)
   func socketWasReconnected()
   func webSocketDidFailWithError(error:String)
}

UPDATE

also add flag @objc

Upvotes: 1

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20369

project-swift.h will not be generated/updated until your application has no error. If the application build fails project-swift.h will not be updated.

Its a kind of dead lock scenario.

Solution :

Step 1: Delete the confirmation of protocol in Objective - C file

#import <Project-Swift.h>

@interface ChatManager () /*<SocketManagerDelegate> delete this*/
@end

Step 2: Now clean and re-build project

Step 3: Now check Project-Swift.h that should now have your protocol declaration

Step 4: Now Confirm the protocol in your Objective -C file and build.

EDIT:

Swift constructs to appear in Project-Swift.h they should either be NSObject or NSObjectProtocol

Change your protocol def to

@objc protocol SocketManagerDelegate : NSObjectProtocol {
   func webSocketDidReceiveMessage(message:Message)
   func socketWasReconnected()
   func webSocketDidFailWithError(error:String)
}

Upvotes: 1

Related Questions