Equality on Objective-C typedef enum in Swift

前端 未结 4 756
误落风尘
误落风尘 2021-02-19 08:01

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:

相关标签:
4条回答
  • 2021-02-19 08:13

    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.

    0 讨论(0)
  • 2021-02-19 08:20

    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.

    0 讨论(0)
  • 2021-02-19 08:34

    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'
    
    0 讨论(0)
  • 2021-02-19 08:35

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

    if (device.deviceType.value == TYPE_BLUETOOTHNA.value)
    
    0 讨论(0)
提交回复
热议问题