将objective-c enum与swift结合。

时间:2022-09-07 09:37:54

I am using the wahoo fitness API (which is in objective-c) in my swift app. I am trying to combine bitwise multiple items from an enum which is not an NS_ENUM. It is defined as:

我正在使用我的swift应用程序中的wahoo fitness API(在objective-c中)。它被定义为:

typedef enum
{
    /** Specifies non-existent sensor. */
    WF_SENSORTYPE_NONE                           = 0,
    /** Specifies the bike power sensor. */
    WF_SENSORTYPE_BIKE_POWER                     = 0x00000001,
    /** Specifies the bike speed sensor. */
    WF_SENSORTYPE_BIKE_SPEED                     = 0x00000002,
    /** Specifies the bike cadence sensor. */
    WF_SENSORTYPE_BIKE_CADENCE                   = 0x00000004,

    ...

} WFSensorType_t;

The following resulted in: 'WFSensorType_t' is not convertible to 'Bool'

下面的结果是:“WFSensorType_t”不能转换为“Bool”

let sensorType = WF_SENSORTYPE_HEARTRATE | WF_SENSORTYPE_BIKE_SPEED | WF_SENSORTYPE_BIKE_CADENCE // WFSensorType_t

The tricky part is that sensorType needs to be passed to another wahoo API object which accepts a WFSensorType_t so I can't wrap the enum into something else otherwise it won't be able to pass it back to the existing API.

棘手的是,sensorType需要传递给另一个接受WFSensorType_t的wahoo API对象,这样我就不能将enum打包到其他东西中,否则它将无法将其传递回现有的API。

Any idea?

任何想法?

1 个解决方案

#1


5  

You can try:

你可以尝试:

let sensorType = WF_SENSORTYPE_HEARTRATE.value | WF_SENSORTYPE_BIKE_SPEED.value | WF_SENSORTYPE_BIKE_CADENCE.value

However sensorType will be inferred by Swift as type UInt8. You cannot declare it as WFSensorType_t

然而,传感器类型将被Swift推断为UInt8类型。您不能将其声明为WFSensorType_t。

#1


5  

You can try:

你可以尝试:

let sensorType = WF_SENSORTYPE_HEARTRATE.value | WF_SENSORTYPE_BIKE_SPEED.value | WF_SENSORTYPE_BIKE_CADENCE.value

However sensorType will be inferred by Swift as type UInt8. You cannot declare it as WFSensorType_t

然而,传感器类型将被Swift推断为UInt8类型。您不能将其声明为WFSensorType_t。