Denny Ferrassoli
Denny Ferrassoli

Reputation: 1725

Equality on Objective-C typedef enum in Swift

I'm working with the Facebook Objective-C SDK in Swift and I'm trying to compare an FBSessionState value with a value from the enum. However I get the compiler error:

Could not find an overload for '==' that accepts the supplied arguments

I'm essentially trying to accomplish:

if state == FBSessionStateOpen { ... }

I'm able to work around this by comparing against the value...

if state.value == FBSessionStateOpen.value { ... }

But I'm wondering if there is a way to make this work more like a Swift enum?

Upvotes: 14

Views: 3817

Answers (4)

Vincil Bishop
Vincil Bishop

Reputation: 1624

Adding to Nikolai Nagorny's answer, this is what worked for me:

if (device.deviceType.value == TYPE_BLUETOOTHNA.value)

Upvotes: 2

lsw
lsw

Reputation: 351

With the Beta4 update, the .value workaround no longer works. There doesn't seem to be another easy workaround without changing Facebook's SDK.

I changed all the Facebook enums to use the NS_ENUM macro, so that you can use Swift syntax the enums.

if FBSession.activeSession().state == .CreatedTokenLoaded

These changes were merged into pgaspar's Facebook fork, which includes other fixes for Swift compatibility.

pod 'Facebook-iOS-SDK', :git => 'https://github.com/pgaspar/facebook-ios-sdk.git'

Upvotes: 4

Nikolai Nagornyi
Nikolai Nagornyi

Reputation: 1461

You could unwrap the enum and constants with '.value' to get the underlying integer, which should be switchable:

switch x.value {
  case Foo.value:
}

Maybe this is a bug and apple fix it in future releases.

Upvotes: 9

Erik
Erik

Reputation: 12858

Swift automatically maps Obj-C enums to its own style of enumName.caseName structure. For example, if the enum is named FBSessionState and there is the FBSessionStateOpen case, it will map as FBSessionState.Open in Swift.

The == operator will work for comparing Swift enums.

Upvotes: -1

Related Questions