What is the ObjC counterpart for _ObjectiveCBridgeable?

I'm trying to implement a transparent ObjC<->Swift bridging for my custom type.

I've conformed Swift type to _ObjectiveCBridgeable, and when imported into ObjC, all usages of it are automatically bridged. But when bridging ObjC usages into Swift, they are still seen as ObjC classes, even though compiler sees conformance to _ObjectiveCBridgeable and offers to insert as.

I could mark ObjC usages as NS_REFINED_FOR_SWIFT and provide necessary wrappers in Swift extensions, but that's a lot of boilerplate code.

Is there something I could use on ObjC side to make ObjC usages bridged to Swift type automatically?

Example code:

@interface Foo : NSObject

@interface Bar: NSObject
@property(nonatomic, copy) Foo *single;
@property(nonatomic, copy) NSArray<Foo *> *array;

void test() {
    Baz* baz = [Baz new];
    Foo *single = baz.single; // OK
    NSArray<Foo *> *array = baz.array; // OK
public struct Foo: _ObjectiveCBridgeable {
    public typealias _ObjectiveCType = __Foo
    public func _bridgeToObjectiveC() -> __Foo { __Foo() }

@objc public class Baz: NSObject {
    @objc public var single: Foo = Foo()
    @objc public var array: [Foo] = []

func test() {
    let bar = Bar()
    let single: Foo = bar.single // ERROR: '__Foo' is not implicitly convertible to 'Foo'; did you mean to use 'as' to explicitly convert?
    let array: [Foo] = bar.array // ERROR: cannot assign value of type '[__Foo]' to type '[Foo]'

_ObjectiveCBridgeable cannot be used outside of the standard library.

Terms of Service

Privacy Policy

Cookie Policy