我很快就知道了,我没有使用objective-c的经验。这是一个Objective-c类,我想在我的swift代码中使用它 WebSocket.h:
#import <Foundation/Foundation.h>
@class HTTPMessage;
@class GCDAsyncSocket;
#define WebSocketDidDieNotification @"WebSocketDidDie"
@interface WebSocket : NSObject
{
dispatch_queue_t websocketQueue;
HTTPMessage *request;
GCDAsyncSocket *asyncSocket;
NSData *term;
BOOL isStarted;
BOOL isOpen;
BOOL isVersion76;
id __unsafe_unretained delegate;
}
+ (BOOL)isWebSocketRequest:(HTTPMessage *)request;
- (id)initWithRequest:(HTTPMessage *)request socket:(GCDAsyncSocket *)socket;
@property (/* atomic */ unsafe_unretained) id delegate;
@property (nonatomic, readonly) dispatch_queue_t websocketQueue;
- (void)start;
- (void)stop;
- (void)sendMessage:(NSString *)msg;
- (void)sendData:(NSData *)msg;
- (void)didOpen;
- (void)didReceiveMessage:(NSString *)msg;
- (void)didClose;
@end
@protocol WebSocketDelegate
@optional
- (void)webSocketDidOpen:(WebSocket *)ws;
- (void)webSocket:(WebSocket *)ws didReceiveMessage:(NSString *)msg;
- (void)webSocketDidClose:(WebSocket *)ws;
@end
我已将上面的头文件添加到XXXX-Bridging-Header.h中,如此
#import "WebSocket.h"
但是当我想尝试实例化WebSocket类时,我在自动完成中看不到任何初始化器:
答案 0 :(得分:1)
尝试替换
- (id)initWithRequest:(HTTPMessage *)request socket:(GCDAsyncSocket *)socket;
带
- (id)initWithRequest:(HTTPMessage *)request socket:(GCDAsyncSocket *)socket NS_DESIGNATED_INITIALIZER;
答案 1 :(得分:1)
我认为您的问题在于您没有导入依赖项:
<html>
<body>
Hello, <%= name -%>
<br/>
</body>
</html>
Swift不知道,因为桥接标头或@class HTTPMessage;
@class GCDAsyncSocket;
都没有导入/包含它们。因此,在Swift中无法访问使用它们的方法。