Swift enum both a string and an int
在这种情况下,我尝试对一些数据进行二进制解码,数据类型既有一个数值,也有一个字符串值和一个名称。我正在考虑使用枚举,例如:
1 2 3 4 5 6 | 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" } |
但我也知道:
我想我想做点什么
可以打印
同样,我知道这可以用一个结构或一个类来完成,但是我对枚举特别感兴趣
或者另一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /// 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)"; } } } |
是否有方法将此结构重构为枚举,以便我使用整数值构造枚举,但将该枚举"读取"为字符串?我很确定答案是否定的。
我想这会对我有好处的。谢谢你自己……:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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" } } } |
使用Swift 4.2,可以使用caseiterable来完成此操作。一种相对简洁的方法是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 " print(Directions.east.asInt()) // prints"2 " print(Directions.east.rawValue) // prints"east " |
你考虑过用字典吗?
1 2 3 4 5 | let targetTrackDict: [Int: String] = [99:"Invalid", 1:"True Track Angle", 2:"Magnetic", 5:"True"] |
请注意,数字代码不必是有序的或连续的。在声明中明确说明字典的类型可以防止以下代码段中出现大量警告或错误。
获取代码名称很容易:
1 2 3 4 5 6 | var code = 2 if let name = targetTrackDict[code] { print("\(name) has code \(code)") } else { print("\(code) is not a valid track type") } |
我还没有找到一个整洁的方法来获取一个名字的代码,但是这样做了:
1 2 3 | let magneticCode = targetTrackDict.first(where: {key, value in value =="Magnetic"})?.key // returns an optional |
你当然会把它打扮成一种功能。您不会自动得到的是您的曲目类型的内部名称,但您需要一个吗?上面这条线在某种程度上是为你做的。