4
votes

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
The problem is the array, not the enum. - matt
Thanks Matt, but is there any way to make it work? This method as is worked fine in Swift 3 when bridged into ObjC even with this array... - Antonin Charvat
I think that was because the nature of bridging was different in Swift 3. - matt
So does that mean it will simply not work in Swift 4 no more? What about some workaround? I have plenty of swift enum arrays bridged to ObjC in my app... - Antonin Charvat
Make them [Int] arrays and map the enums to their raw values. That works because the Ints are mapped to NSNumber, which is an object. - matt

1 Answers

3
votes

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.