简体   繁体   中英

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:

在此处输入图片说明

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;

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)
}

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)
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM