我试图让一个快速的3结构符合_ObjectiveCBridgeable,但我不确定我还需要什么来满足协议。下面是我的结构和_ObjectiveCBridgeable一致性。我错过了一些东西,但我不确定它是什么。
struct Box {
let contents: Any
}
extension Box: _ObjectiveCBridgeable {
typealias _ObjectiveCType = thing;
init(fromObjectiveC source: _ObjectiveCType) {
contents = source.contents
}
static func _isBridgedToObjectiveC() -> Bool {
return true
}
static func _getObjectiveCType() -> Any.Type {
return _ObjectiveCType.self
}
func _bridgeToObjectiveC() -> Box._ObjectiveCType {
return thing(contents: self.contents)
}
static func _forceBridgeFromObjectiveC(_ source: Box._ObjectiveCType, result: inout Box?) {
result = Box(contents: source.contents)
}
static func _conditionallyBridgeFromObjectiveC(_ source: Box._ObjectiveCType, result: inout Box?) -> Bool {
_forceBridgeFromObjectiveC(source, result: &result)
return true
}
}
// Objc
@interface thing : NSObject
@property (readonly) id contents;
-(instancetype)initWithContents:(id)contents;
@end
@implementation thing
- (instancetype)initWithContents:(id)contents {
if ((self = [super init])) {
_contents = contents;
}
return self;
}
@end
答案 0 :(得分:3)
正如下划线所示,_ObjectiveCBridgeable
是私密的。其目的是“满足将Objective-C对象类型桥接到Swift值类型的特定需求”。你不能为自己的类型采用它;它通过“引擎盖下的编译器魔法”工作。
有proposal on the table提供公开版本,但尚未实施。