如何使用 objective-c 将自定义初始化实现公开给接口?

How do I expose custom init implememntation to interface using objective-c?

我简单地定义了 Swift 协议,如下所示:

@objc protocol MobileKeyable {
   var assaAbloyLockServiceCode: Int { get }
}

然后我在我的 objective-c class 中使用它:

- (id)initWithComposition: (id<MobileKeyable>)composition {
    self = [super init];
    if (self) {
        _lockServiceCodes = @[@([composition assaAbloyLockServiceCode])];
    }
    return self;
}

然后我想在 Swift 中像这样初始化控制器:

let composition = //here MobileKeyable
MobileKeysController(composition: composition)

所以我需要将该初始化程序公开给 objective-c 接口:

#import "MyProject-Swift.h"
@interface MobileKeysController : NSObject <MobileKeysManagerDelegate>
- (instancetype) initWithComposition: (id<MobileKeyable>)composition;
@end

但是这里我有一个错误:MyProject-Swift.h file not found。为什么?

也许 Xcode 试图避免一些循环...只是假设。试试前向声明

@protocol MobileKeyable;

@interface MobileKeysController : NSObject <MobileKeysManagerDelegate>
- (instancetype) initWithComposition: (id<MobileKeyable>)composition;
@end