Swift 类 和变量未分配并且从 Objective-C 可见
Swift classes and variable are not alloc and visible from Objective-C
我尝试使用 Swift 版本 4.2 和 5.0,结果相同。 Xcode 说当 Swift 对象从 Objective-C 代码初始化和使用时出错。
库 SwiftR 是最新的。使用 Xcode 11.5
@import SwiftR;
@interface CHSignalrService ()
@property (strong, nonatomic) SignalR *srConnection;
@property (strong, nonatomic) Hub * chatHub;
@property (strong, nonatomic) Hub * orderHub;
@property (strong, nonatomic) Hub * onlineInterpreterHub;
@end
@implementation CHSignalrService
#pragma makr - Lifecycle
- (void)startConnectionWithAccessToken:(NSString *)at {
if (self.srConnection != nil) {
return;
}
//below is error: No visible @interface for 'SignalR' declares the selector 'init:connectionType:'
self.srConnection = [[SignalR alloc] init:kSignalRBaseURLString connectionType:ConnectionTypeHub];
NSMutableDictionary *headersDict = [[NSMutableDictionary alloc] initWithDictionary:@{@"Authorization":at, @"Content-Type":@"application/json"}];
//below is error: Property 'headers' not found on object of type 'SignalR *'
self.srConnection.headers = headersDict;
//below is error: No visible @interface for 'Hub' declares the selector 'init:'
self.onlineInterpreterHub = [[Hub alloc] init:@"OnlineInterpreterHub"];
[self.onlineInterpreterHub on:@"interpreterConnected" callback:^(NSArray * array) {
[[NSNotificationCenter defaultCenter] postNotificationName:kOnlineInterpreterNotification object:array];
}];
self.chatHub = [[Hub alloc] init:@"ChatHub"];
__weak typeof(self)weakSelf = self;
[self.chatHub on:@"NobodyAnswered" callback:^(NSArray * array) {
[weakSelf NobodyAnswered:nil];
}];
}
在SignalR
class声明之前添加@objc
声明。
供参考 - https://pinkstone.co.uk/how-to-use-swift-classes-in-objective-c/
像这样:
@objc
open class SignalR: NSObject, SwiftRWebDelegate {
static var connections = [SignalR]()
希望这对你有用。
之前没有 @objc
声明:
谢谢:)
我尝试使用 Swift 版本 4.2 和 5.0,结果相同。 Xcode 说当 Swift 对象从 Objective-C 代码初始化和使用时出错。 库 SwiftR 是最新的。使用 Xcode 11.5
@import SwiftR;
@interface CHSignalrService ()
@property (strong, nonatomic) SignalR *srConnection;
@property (strong, nonatomic) Hub * chatHub;
@property (strong, nonatomic) Hub * orderHub;
@property (strong, nonatomic) Hub * onlineInterpreterHub;
@end
@implementation CHSignalrService
#pragma makr - Lifecycle
- (void)startConnectionWithAccessToken:(NSString *)at {
if (self.srConnection != nil) {
return;
}
//below is error: No visible @interface for 'SignalR' declares the selector 'init:connectionType:'
self.srConnection = [[SignalR alloc] init:kSignalRBaseURLString connectionType:ConnectionTypeHub];
NSMutableDictionary *headersDict = [[NSMutableDictionary alloc] initWithDictionary:@{@"Authorization":at, @"Content-Type":@"application/json"}];
//below is error: Property 'headers' not found on object of type 'SignalR *'
self.srConnection.headers = headersDict;
//below is error: No visible @interface for 'Hub' declares the selector 'init:'
self.onlineInterpreterHub = [[Hub alloc] init:@"OnlineInterpreterHub"];
[self.onlineInterpreterHub on:@"interpreterConnected" callback:^(NSArray * array) {
[[NSNotificationCenter defaultCenter] postNotificationName:kOnlineInterpreterNotification object:array];
}];
self.chatHub = [[Hub alloc] init:@"ChatHub"];
__weak typeof(self)weakSelf = self;
[self.chatHub on:@"NobodyAnswered" callback:^(NSArray * array) {
[weakSelf NobodyAnswered:nil];
}];
}
在SignalR
class声明之前添加@objc
声明。
供参考 - https://pinkstone.co.uk/how-to-use-swift-classes-in-objective-c/
像这样:
@objc
open class SignalR: NSObject, SwiftRWebDelegate {
static var connections = [SignalR]()
希望这对你有用。
之前没有 @objc
声明:
谢谢:)