0

Equatable enum created in Swift is not available in Objective-C.

I tried as below

@objc enum ViewModel: Equatable {
        case heading(String)
        case options(id: String, title: String, enabled: Bool)

        #if swift(>=4.1)
        #else
        static func ==(lhs: ViewModel, rhs: ViewModel) -> Bool {
            switch (lhs, rhs) {
            case (let .heading(lhsString), let .heading(rhsString)):
                return lhsString == rhsString
            case (let .options(lhsId, lhsTitle, lhsEnabled), let .options(rhsId, rhsTitle, rhsEnabled)):
                return (lhsId, lhsTitle, lhsEnabled) == (rhsId, rhsTitle, rhsEnabled)
            default:
                return false
            }
        }
        #endif
    }

without @objc flag, it works fine, but with @objc not working.

Error says:

'@objc' enum must declare an integer raw type

koen
  • 5,383
  • 7
  • 50
  • 89
Sheshnath
  • 3,293
  • 1
  • 32
  • 60
  • 3
    Compare https://stackoverflow.com/q/30480338/1187415: *“@objc enums must declare an integer raw type, and cannot be generic or use associated values.”* – Martin R Dec 12 '19 at 12:43
  • any right approach to handle this situation? – Sheshnath Dec 12 '19 at 12:44
  • Did you try the suggestions in that link? – koen Dec 12 '19 at 12:58
  • 1
    The key fact is not the `Equatable`. It's the use of associated values. ObjC doesn't support that. You'll almost certainly need to use a class for this problem. – Rob Napier Dec 12 '19 at 13:51

0 Answers0