如何将 Java 枚举转换为 Swift 枚举?

How to convert Java enum to Swift enum?

下面是我的 java 枚举。 我想将其转换为 Swift 枚举。 你能帮我迁移吗?

package com.lifeplus.Pojo.Enum;

public enum GattServiceEnum {
 
    CURR_TIME_SERVICE("00001805-0000-1000-8000-00805f9b34fb", "current_time"),
    DEVICE_INFORMATION_SERVICE("0000180a-0000-1000-8000-00805f9b34fb", "device_information"),
    PULSE_OXY_SERVICE("00001822-0000-1000-8000-00805f9b34fb", "pulse_oximeter"),
    CUSTOM_SERVICE("4C505732-5F43-5553-544F-4D5F53525600", "Custom Service");

    private final String _id;
    private final String _desc;

    GattServiceEnum(String pId, String pDesc) {
        _id = pId;
        _desc = pDesc;
    }

    public String getId() {
        return _id;
    }

    public String getDesc() {
        return _desc;
    }
}

请帮我把它转换成 Java。

试试这个,希望它对你有用:

public enum GattServiceEnum {
    
    case myNameCase(id: String, desc: String)
    case anotherCase
    case onlyIdCase(id: String)
    case onlyDescCase(desc: String)
    
    public func getId() -> String? {
        switch self {
        case .myNameCase(let id, _),
             .onlyIdCase(let id):
            return id
            
        case .anotherCase, .onlyDescCase:
            return nil
        }
    }
    
    public func getDesc() -> String? {
        switch self {
        case .myNameCase(_, let desc),
             .onlyDescCase(let desc):
            return desc
            
        case .anotherCase, .onlyIdCase:
            return nil
        }
    }
}

// create
let myEnumObject = GattServiceEnum.myNameCase(id: "123", desc: "hi")

// get id
print(myEnumObject.getId())

// get desc
print(myEnumObject.getDesc())

不需要使用enum。您可以简单地使用 struct

public struct GattService {

    public let id: String
    public let desc: String

    // You can optionally provide the get functions, but it is simpler just to access the properties directly

    public func getId() -> String {
        return id
    }

    public func getDesc() -> String {
        return desc
    }
}