我正在使用wahoo Fitness API,它定义了以下Objective-C枚举:
typedef enum { /** No active connection. */ WF_SENSOR_CONNECTION_STATUS_IDLE, /** The connection is in process of being established. */ WF_SENSOR_CONNECTION_STATUS_CONNECTING, /** The sensor connection is established and active. */ WF_SENSOR_CONNECTION_STATUS_CONNECTED, /** The connection was interrupted (usually occurs when fisica is disconnected). */ WF_SENSOR_CONNECTION_STATUS_INTERRUPTED, /** The connection is in process of being disconnected. */ WF_SENSOR_CONNECTION_STATUS_DISCONNECTING, } WFSensorConnectionStatus_t;
我找不到快速使用它的方法。我首先尝试对其进行切换/操作,但未成功。我正要继续进行以下操作:
var connState : WFSensorConnectionStatus_t = WF_SENSOR_CONNECTION_STATUS_IDLE ... if( connState == WF_SENSOR_CONNECTION_STATUS_IDLE){
但是它不能编译:
'WFSensorConnectionStatus_t' is not convertible to 'NSObject'
任何解决方法?我阅读使用WFSensorConnectionStatus_t.WF_SENSOR_CONNECTION_STATUS_IDLE,WF_SENSOR_CONNECTION_STATUS_IDLE.value但在xcode beta-4中不起作用。
WFSensorConnectionStatus_t.WF_SENSOR_CONNECTION_STATUS_IDLE
WF_SENSOR_CONNECTION_STATUS_IDLE.value
如您所说,从.beta 4开始,使用.value获取基础整数的解决方法不再起作用。
不幸的是an enum不能从Objective-C转移到Swift,它必须是an NS_ENUM。
enum
NS_ENUM
我在一个需要enum从Objective-C框架中进行移植并在Swift中使用它的项目中,具有与您相同的设置。
我所做的解决方法是创建一个包含的Objective-C类别,NS_ENUM然后将值从框架enum传递到自己的NS_ENUM。
将类别导入桥接头中,您应该可以像平常一样使用enum。
像这样:
typedef NS_ENUM(NSUInteger, ConnectionStatus) { ConnectionStatusIdle } - (ConnectionStatus)connectionStatus { if [self getConnectionStatus] == WF_SENSOR_CONNECTION_STATUS_IDLE { return ConnectionStatusIdle } }
然后,您应该可以像这样使用它:
switch myObject.connectionStatus() { case .Idle: // do something break }