我正在使用wahoo fitness API,它定义了以下目标: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中不起作用.
正如你所说,使用.value获取基础整数的解决方法不再像Beta 4那样运行.
原文链接:https://www.f2er.com/swift/319653.html不幸的是,枚举不能从Objective-C转移到Swift,它需要是NS_ENUM.
我有一个与您在项目中相同的设置,我需要一个Objective-C框架的枚举,并在Swift中使用它.
我所做的解决方法是创建一个包含NS_ENUM的Objective-C类,并将值从框架枚举传递给我自己的NS_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 }