Swift 枚举同时包含字符串和整数。

20 浏览
0 Comments

Swift 枚举同时包含字符串和整数。

我有一个这样的情况,我正在尝试对一些数据进行二进制解码,数据类型具有数字值,字符串值和名称。我考虑使用诸如以下的枚举:

enum TARGET_TRACK_TYPE : String {
    case TT_INVALID          = "Invalid"
    case TT_TRUE_TRACK_ANGLE = "True Track Angle"
    case TT_MAGNETIC         = "Magnetic"
    case TT_TRUE             = "True"
}

但我也知道:

TT_INVALID = 0TT_TRUE_TRACK_ANGLE = 1等。是否有一种简单的方法将这两种“内容”,即字符串和数字值封装到一个枚举构造中,还是我需要制作一些处理这些的结构体/类?

我想要做的事情就像:

let a = TARGET_TRACK_TYPE.rawValue(value: 2)

println(a)

这将输出True Track Angle

同样,我知道这可以用结构体或类来完成,但我特别感兴趣的是枚举。

另一个例子:

/// Emitter Category is defined in section 3.5.1.10 of the GDL90 Spec
struct EmitterCategory {
let category : Int
func getString() -> String {
    switch(category) {
    case 0:
        return "No aircraft type information";
    case 1:
        return "Light";
    case 2:
        return "Smalle";
    case 3:
        return "Large";
    case 4:
        return "High Vortex Large";
    case 5:
        return "Heavy";
    case 6:
        return "Highly Manuverable";
    case 7:
        return "Rotorcraft";
    case 8:
        return "(Unassigned)";
    case 9:
        return "Glider/sailplane";
    case 10:
        return "Ligther than air";
    case 11:
        return "Parachutist/sky diver";
    case 12:
        return "Ultra light/hang glider/paraglider";
    case 13:
        return "(Unassigned)";
    case 14:
        return "Unmanned aerial vehicle";
    case 15:
        return "Space/transatmospheric vehicle";
    case 16:
        return "(Unassigned)";
    case 17:
        return "Surface vehicle - emergency vehicle";
    case 18:
        return "Surface vehicle - service vehicle";
    case 19:
        return "Point obstacle";
    case 20:
        return "Cluster Obstacle";
    case 21:
        return "Line Obstacle";
    default:
        return "(reserved)";
    }
}
}

是否有一种方法将这个结构体重构为枚举,以使我构造枚举时使用整数值,但我“读取”枚举时是一个字符串? 我相当确定答案是否定的。

admin 更改状态以发布 2023年5月21日
0
0 Comments

在Swift 4.2中,可以使用CaseIterable来实现。相对简洁的方式是执行以下操作:

enum Directions: String, CaseIterable {
    case north, south, east, west
    static var asArray: [Directions] {return self.allCases}
    func asInt() -> Int {
        return Directions.asArray.firstIndex(of: self)!
    }
}
print(Directions.asArray[2])
// prints "east\n"
print(Directions.east.asInt())
// prints "2\n"
print(Directions.east.rawValue)
// prints "east\n"

0
0 Comments

我认为这样做对我来说是可以的。谢谢我自己.. 🙂

protocol GDL90_Enum  {
      var description: String { get }
}
enum TARGET_ADDRESS_TYPE : Int, GDL90_Enum {
   case ADSB_ICAO_ADDRESS = 0
   case ADSB_SELF_ADDRESS = 1
   case TISB_ICAO = 2
   case TISB_TRACK_ID = 3
   case SURFACE_VEHICLE = 4
   case GROUND_STATION = 5
   var description: String {
      switch self {
   case .ADSB_ICAO_ADDRESS:
      return "ADS-B with ICAO address"
   case .ADSB_SELF_ADDRESS:
      return "ADS-B with Self-assigned address"
   case .TISB_ICAO:
      return "TIS-B with ICAO address"
   case .TISB_TRACK_ID:
         return "TIS-B with track file ID"
   case .SURFACE_VEHICLE:
         return "Surface Vehicle"
   case .GROUND_STATION:
         return "Ground Station Beacon"
   default:
         return "Reserved"
      }
   }
}

0