Классы и переменные Swift не являются allo c и видны из Objective- C - PullRequest
0 голосов
/ 17 июня 2020

Я пробовал использовать 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];
    }];
}

1 Ответ

0 голосов
/ 17 июня 2020

Добавить объявление @objc перед объявлением класса SignalR. Для справки - https://pinkstone.co.uk/how-to-use-swift-classes-in-objective-c/

Примерно так:

@objc
open class SignalR: NSObject, SwiftRWebDelegate {
    static var connections = [SignalR]()

Надеюсь у вас сработает.

Раньше не было @objc декларация: enter image description here

Спасибо :)

...