Swift中Objective-C typedef枚举的平等性

时间:2022-09-07 09:38:00

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:

我正在使用Swift中的Facebook Objective-C SDK,我正在尝试将FBSessionState值与枚举值进行比较。但是我得到了编译器错误:

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?

但我想知道是否有办法让这项工作更像是一个Swift枚举?

4 个解决方案

#1


9  

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

您可以使用'.value'解包enum和常量以获取底层整数,该整数应该是可切换的:

switch x.value {
  case Foo.value:
}

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

也许这是一个错误,苹果在未来的版本中修复它。

#2


4  

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

通过Beta4更新,.value变通方法不再有效。如果不改变Facebook的SDK,似乎没有其他简单的解决方法。

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

我更改了所有Facebook枚举以使用NS_ENUM宏,以便您可以使用Swift语法枚举。

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

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

这些变化被合并到pgaspar的Facebook分支中,其中包括Swift兼容性的其他修复程序。

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

#3


2  

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

除了Nikolai Nagorny的答案之外,这对我有用:

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

#4


-1  

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.

Swift自动将Obj-C枚举映射到它自己的enumName.caseName结构样式。例如,如果枚举名为FBSessionState并且存在FBSessionStateOpen情况,则它将在Swift中映射为FBSessionState.Open。

The == operator will work for comparing Swift enums.

==运算符将用于比较Swift枚举。

#1


9  

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

您可以使用'.value'解包enum和常量以获取底层整数,该整数应该是可切换的:

switch x.value {
  case Foo.value:
}

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

也许这是一个错误,苹果在未来的版本中修复它。

#2


4  

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

通过Beta4更新,.value变通方法不再有效。如果不改变Facebook的SDK,似乎没有其他简单的解决方法。

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

我更改了所有Facebook枚举以使用NS_ENUM宏,以便您可以使用Swift语法枚举。

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

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

这些变化被合并到pgaspar的Facebook分支中,其中包括Swift兼容性的其他修复程序。

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

#3


2  

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

除了Nikolai Nagorny的答案之外,这对我有用:

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

#4


-1  

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.

Swift自动将Obj-C枚举映射到它自己的enumName.caseName结构样式。例如,如果枚举名为FBSessionState并且存在FBSessionStateOpen情况,则它将在Swift中映射为FBSessionState.Open。

The == operator will work for comparing Swift enums.

==运算符将用于比较Swift枚举。