Bridge Swift 4 enum to Objective C

Viewed 1202

EDIT: I apologize all of you, this was all the time my fault - it wasn't possible even in Swift 3. I confused myself big time. Sorry for this question.

Apparently bridging array of enum values to Objective C is no longer possible in Swift 4, not even using @objc annotation:

@objc open func removeCacheAfterDelay(_ delay: Double, forType types: [CacheManagerType]) {
}

@objc public enum CacheManagerType: Int, RawRepresentable {
    case credit
    case debit
    case transactionHistory
}

Following error is displayed at the removeCacheAfterDelay function:

Method cannot be marked @objc because the type of the parameter 2 cannot be represented in Objective-C

Am I missing something? Is there any workaround?

1 Answers

You have to think like Objective-C. What could this declaration possibly mean when translated into Objective-C? In particular, what would [CacheManagerType] mean? It would have to be an array, i.e. an NSArray, containing CacheManagerType objects.

But that's impossible. CacheManagerType is an enum. An Int enum bridged to Objective-C is turned into an Objective-C enum. For your method declaration to work in Objective-C, Objective-C would need to be able to understand the concept of an array of enums — and it doesn't. In Objective-C, an enum is not an object, but an NSArray can hold only objects.

Related