Using non NS_ENUM objective-C enum in swift

前端 未结 4 1859
你的背包
你的背包 2020-12-10 03:18

I am using the wahoo fitness API and it defines the following objective-C enum:

typedef enum
{
    /** No active connection. */
    WF_SENSOR_CONNECTION_STAT         


        
4条回答
  •  再見小時候
    2020-12-10 03:54

    C-style enums import in Swift like UInt32. So you can do something like:

    let state = unsafeBitCast(WF_SENSOR_CONNECTION_STATUS_IDLE, UInt32.self)
    if state == unsafeBitCast(WF_SENSOR_CONNECTION_STATUS_IDLE, UInt32.self) {
        //do something
    }
    

    Upd: In Swift 2.1 (Xcode 7.1 beta 2) all C-style enums conforms Equatable and you can now use it like:

    let state = WF_SENSOR_CONNECTION_STATUS_IDLE
    if state == WF_SENSOR_CONNECTION_STATUS_IDLE {
        //do something
    }
    

    Profit :)

提交回复
热议问题