Using non NS_ENUM objective-C enum in swift

前端 未结 4 1852
你的背包
你的背包 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:50

    The workaround to use .value to get the underlying integer doesn't work anymore as of Beta 4, as you said.

    Unfortunately an enum is not transferrable to Swift from Objective-C, it needs to be an NS_ENUM.

    I have the same setup as you in a project where I need the enum from an Objective-C framework and use it in Swift.

    The workaround I did was to create an Objective-C category that contains an NS_ENUM and there I transfer the values from the framework enum to my own NS_ENUM.

    Import the category in your bridging header and you should be able to use the enum as you normally would do.

    Something like this:

    typedef NS_ENUM(NSUInteger, ConnectionStatus) {
        ConnectionStatusIdle
    }
    
    - (ConnectionStatus)connectionStatus {
        if [self getConnectionStatus] == WF_SENSOR_CONNECTION_STATUS_IDLE {
            return ConnectionStatusIdle
        }
    }
    

    Then you should be able to use it like this:

    switch myObject.connectionStatus() {
        case .Idle:
            // do something
            break
    }
    
    0 讨论(0)
  • 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 :)

    0 讨论(0)
  • 2020-12-10 03:55

    Here is the final complete solution:

    WFSensorConnection+SensorConnectionEnumCategory.h
    

    :

    #import <Foundation/Foundation.h>
    
    #import <WFConnector/WFConnector.h>
    
    @interface WFSensorConnection (SensorConnectionEnumCategory)
    
    typedef NS_ENUM(NSUInteger, ConnectionStatus) {
        ConnectionStatusIdle,
        ConnectionStatusConnecting,
        ConnectionStatusConnected,
        ConnectionStatusInterrupted,
        ConnectionStatusDisconnecting
    };
    
    - (ConnectionStatus) swift_connectionStatus;
    
    @end
    

    WFSensorConnection+SensorConnectionEnumCategory.m
    

    :

    #import "WFSensorConnection+SensorConnectionEnumCategory.h"
    
    @implementation WFSensorConnection (SensorConnectionEnumCategory)
    
    - (ConnectionStatus) swift_connectionStatus{
        if ( [self connectionStatus] == WF_SENSOR_CONNECTION_STATUS_IDLE ){
            return ConnectionStatusIdle;
        } else if ( [self connectionStatus] == WF_SENSOR_CONNECTION_STATUS_CONNECTING ){
            return ConnectionStatusConnecting;
        } else if ( [self connectionStatus] == WF_SENSOR_CONNECTION_STATUS_CONNECTED ){
            return ConnectionStatusConnected;
        } else if ( [self connectionStatus] == WF_SENSOR_CONNECTION_STATUS_DISCONNECTING ){
            return ConnectionStatusDisconnecting;
        } else if ( [self connectionStatus] == WF_SENSOR_CONNECTION_STATUS_INTERRUPTED ){
            return ConnectionStatusInterrupted;
        }
        return 0;
    }
    
    @end
    

    Bridging-Header.h
    

    :

    #import "WFSensorConnection+SensorConnectionEnumCategory.h"
    

    Usage:

    var sensorConnection: WFSensorConnection?
    var connState : ConnectionStatus = ConnectionStatus.Idle
    connState = sensorConnection!.swift_connectionStatus()
    switch connState {
        case ConnectionStatus.Idle:
    ...
    }
    
    0 讨论(0)
  • 2020-12-10 04:10

    Note that there is a CoreFoundation type that is similar to NS_ENUM called CF_ENUM. I've used it on my framework that is mainly C. And yes, Swift does translate it to a Swift enum.

    There is also something similar for NS_OPTIONS called CF_OPTIONS.

    0 讨论(0)
提交回复
热议问题