如何在Swift中转换@synthesize协议变量?

问题描述

我必须将一些文件从Objective-C转换为Swift。问题是,我真的对协议和被称为相同的类以及@synthetize协议变量(我不知道如何初始化或如何将其转换为Swift)之间感到困惑。我将尝试制定一个简单的方案。

// Protocols.h
ProtocolA {
 - functionA
}

ProtocolB {
 - variableNameB : ProtocolA // Can a variable be of type protocol? shouldn't it be of type class and that class is the one that follows the protocol?
 - functionB
}
------------------------

// Class1.m
Class1: ProtocolB {
// This is what I don't understand at all,how is initiating itself? There are not initialisers for Protocols.
@synthesize variableNameB = _variableNameB 
}

我遇到的错误

如果我尝试只写var variableNameB: ProtocolA而没有给出任何值,我将在Class1的init方法中得到它:

属性'self.variableNameB'未在隐式生成时初始化 super.init调用

如果我尝试通过self.variableNameB = ProtocolA()在Class1初始化中初始化它,则会得到:

ProtocolA无法构建,因为它不可访问 初始化程序

如果我尝试将variableNameB设为可选,那么就不强制初始化它,我得到了:

getter提供的Object-C方法“ variableNameB”用于 'variableNameB'与可选要求获取器冲突 协议ProtocolB中的“ variableNameB”

setter提供的Object-C方法“ setvariableNameB:” variableNameB与的可选需求设置程序冲突 协议ProtocolB中的“ variableNameB”


所有用于扩展上下文,目标C和SWIFT的文件

TermsAndConditionsReader.h

@class UserAccountManager;
@class TermsAndConditionsManager;
@protocol TermsAndConditionsManagerObserver;

NS_ASSUME_NONNULL_BEGIN

@interface TermsAndConditionsReader : GenericInteractor <TermsAndConditionsReader>

INteraCTOR_INIT_UNAVAILABLE;

- (instancetype)initWithUserAccountManager:(UserAccountManager*)userAccountManager
                 termsAndConditionsManager:(TermsAndConditionsManager*)termsAndConditionsManager
                                     appId:(NSUInteger)appId NS_DESIGNATED_INITIALIZER;


@property (nonatomic,strong,readonly) UserAccountManager* userAccountManager;
@property (nonatomic,readonly) TermsAndConditionsManager* termsAndConditionsManager;

@end

NS_ASSUME_NONNULL_END

TermsAndConditionsReader.m

@interface TermsAndConditionsReader () <TermsAndConditionsManagerObserver>

  @property (nonatomic,assign) NSUInteger appId;

@end

@implementation TermsAndConditionsReader
  @synthesize listener = _listener;

- (instancetype)initWithUserAccountManager:(UserAccountManager*)userAccountManager
                 termsAndConditionsManager:(TermsAndConditionsManager*)termsAndConditionsManager
                                     appId:(NSUInteger)appId {
    self = [super init];

    if (self) {
        _userAccountManager = userAccountManager;
        _termsAndConditionsManager = termsAndConditionsManager;
        [termsAndConditionsManager addTermsAndConditionsManagerObserverWithObserver:self];
        _appId = appId;
    }

    return self;
}

- (BOOL)areTermsAndConditionsAccepted {
    id<UserAccount> userAccount = self.userAccountManager.userAccount;
    return [userAccount hasAcceptedTermsAndConditionsForApp:self.appId];
}

#pragma mark - TermsAndConditionsManagerObserver

- (void)termsAndConditionsManagerUserNeedsToAcceptNewTermsWithSender:(TermsAndConditionsManager * _Nonnull)sender {
    [self.listener termsAndConditionsReaderNewTermsAndConditionsHasToBeAccepted:self];
}

@end

TermsAndConditionsReader.swift ->这可能是错误的,这是我尝试转换的方式

@objc public class TermsAndConditionsReader: GenericInteractor,TermsAndConditionsReaderProtocol,TermsAndConditionsManagerObserver {

let userAccountManager: UserAccountManager
let termsAndConditionsManager: TermsAndConditionsManager
let appId: UInt
public var listener: AnyObject & TermsAndConditionsListener

@objc public init(userAccountManager: UserAccountManager,termsAndConditionsManager: TermsAndConditionsManager,appId: UInt) {
    self.appId = appId
    self.userAccountManager = userAccountManager
    self.termsAndConditionsManager = termsAndConditionsManager
    termsAndConditionsManager.addTermsAndConditionsManagerObserver(observer: self) // -> It complains here because I am calling self before having initialised listener
}

// MARK: - TermsAndConditionsReaderProtocol

@objc public func areTermsAndConditionsAccepted() -> Bool {
    return self.userAccountManager.userAccount?.hasAcceptedTermsAndConditions(forApp: self.appId) ?? false
}

// MARK: - TermsAndConditionsManagerObserver

@objc public func termsAndConditionsManagerUserNeedsToAcceptNewTerms(sender: TermsAndConditionsManager) {
    // call listener here:
}
}

InteractorsTermsAndConditions.h

@protocol TermsAndConditionsListener <NSObject>

- (void)termsAndConditionsReaderNewTermsAndConditionsHasToBeAccepted:(id<TermsAndConditionsReader>)sender;

@end


@protocol TermsAndConditionsReader <NSObject>

@property (nonatomic,weak,nullable) id<TermsAndConditionsListener> listener;

- (BOOL)areTermsAndConditionsAccepted;

@end

InteractorsTermsAndConditions.swift ->这就是我的翻译方式

@objc public protocol TermsAndConditionsListener {
    @objc func termsAndConditionsReaderNewTermsAndConditionsHasToBeAccepted(sender: TermsAndConditionsReaderProtocol)

}


@objc public protocol TermsAndConditionsReaderProtocol {

    @objc var listener: AnyObject &  TermsAndConditionsListener { get set }
    @objc func areTermsAndConditionsAccepted() -> Bool

}

解决方法

您不能在ProtocolA实现中将Class1用作类型;您需要声明另一个Class2: ProtocolA并将其用作您的variableNameB

在代码的上下文中,您需要声明

// rename your listener protocol
protocol TermsAndConditionsListenerProtocol {
    // whatever requirements
}

// implement a concrete type that conforms to this protocol
class TermsAndConditionsListener: TermsAndConditionsListenerProtocol {
    // implement requirements 
    // as well as any init you need
    init(hello: String) { /* ... */ }
}

// adjust your reader protocol
protocol TermsAndConditionsReaderProtocol {
    var listener: TermsAndConditionsListenerProtocol { get set }
    func areTermsAndConditionsAccepted() -> Bool
}

// implement a concrete reader that conforms to reader protocol
class TermsAndConditionsReader: TermsAndConditionsReaderProtocol {
    // here you use a concrete type that conforms to the listener protocol
    var listener: TermsAndConditionsListener = .init(hello: "world")
    
    // + other requirements
}