Provide an Objective-C compatibility to non-compliant Swift types

68 Views Asked by At

I am trying to understand what would be a good pattern for providing an Objective-C compatibility to non-compliant Swift types.

Consider the following example:

class Model {
    var b: Bool? // Optional Bool cannot be marked as @objc
    var i: Int? // Optional Int cannot be marked as @objc
    var e: MyEnum // Swift enums, except for Integer type, cannot be marked as @objc
}

My current naive implementation is:

extension Model {
    @objc func setB(_ value: Bool) { b = value }
    @objc func unsetB() { b = nil }
    @objc var isB: NSNumber? { guard let b = b else {return nil }; return NSNumber(value: b) }
}

That works, but with many properties, this is very tedious to define, and even worse on usage from Objective-C (while in Swift all properties can be provided on init, in Objective-C they would have to be set / unset one by one).

So are there any better patterns to provide an Objective-C compatibility to the properties of non-compliant Swift types?

Any tips are welcome. Thanks.

0

There are 0 best solutions below