Home > database >  Can I add enum value from another Swift enum?
Can I add enum value from another Swift enum?

Time:10-01

I have enum with volume types and enum of drink types

enum AlcoholVolumeType: String {

case portion30
case portion40
case portion50

case wineglass100
case wineglass125
case wineglass150
case glass175
case glass200
case glass300

case beerBottle330
case beerBottle355
case beerGlass400
case britishBank440
case americanPint473
case beerBottle500
case britishPint568
case bomber650

And drink enum

enum AlcoholType: String {

case champagne
case wineRed
case wineWhite
case winePink
case portWine

static var wineTypes: [AlcoholType] { [.champagne, .wineRed, .wineWhite, .winePink, .portWine] }
var isWine: Bool { AlcoholType.wineTypes.contains(self) }

case beerLight
case beerDark
case beerUnfiltered
case cider
case ale

Also i have custom drink types, so i need to add static value to enum with volume types

I tried to do this but i have an error

enum AlcoholVolumeType: String {

case \(AlcoholType)wineglass150
// Consecutive declarations on a line must be separated by ';'
}

I will try to explain in more detail enter image description here

The application has the function of adding your own alcohol

If we have two types of custom alcohol, and the same containers are selected, then a bug occurs with the statistics, since it is calculated by the type of container, and we have one container for two types of alcohol, the following two screenshots show this

enter image description here enter image description here

That is, I need to create a container type for each new type of alcohol so that one is not used for all, and there is no such bug in the statistics

CodePudding user response:

Maybe you are talking about enums with associated values?
Something like this:

enum Wine {
    case red
    case white
    case pink
}

enum Beer {
    case light
    case dark
    case unfiltered
}

enum AlcoholType {
   case wine(type: Wine)
   case beer(type: Beer)
}

let alcohol: AlcoholType = .wine(type: .red)
switch alcohol {
    case .wine(type: let type):
       switch type { 
           case .red:
              break
           case .white:
              break
           case .pink:
              break
       }
   case .beer(type: let type): 
       switch type {
          case .light: 
              break
          case .dark: 
              break
          case .unfiltered: 
              break
       }
}

CodePudding user response:

Its hard to decide what do you need. If you need to store drink type and volume in one place - may be would be better to use structures and enums with associated values:

enum Wine: String {
    case red
    case white
    case pink
}

enum Beer: String {
    case light
    case dark
    case unfiltered
}

enum AlcoholType {
   case wine(type: Wine)
   case beer(type: Beer)

   func getName() -> String {
      switch self {
      case .wine(type: let type):
         return "\(type.rawValue) wine"
      case .beer(type: let type):
         return "\(type.rawValue) beer"   
      }
}
enum AlcoholVolumeType {
   case glass(Int)
   case portion(Int)
   case bottle(Int)
}

struct Drink {
    let alcohol: AlcoholType
    let volume: AlcoholVolumeType
} 

if you need to check matching - you can extend this struct:

struct Drink {
    let alcohol: AlcoholType
    let volume: AlcoholVolumeType

   init?(alcohol: AlcoholType, volume: AlcoholVolumeType) {
      guard validCombination(of: alcohol, volume) else { return nil}
      let alcohol = alcohol
      let volume = volume
   }

   func validCombination(of alcohol: AlcoholType, _ volume: AlcoholVolumeType) -> Bool {
      switch (alcohol, volume) {
      case (.wine(_), .glass(let size):
         return (size == 100) || (size == 125)
      default:
         return false
      }
   }

   func getDescription() -> String {
      return alcohol.getName()
   }
}

let validCase = Drink?(alcohol: .wine(.red), volume: .glass(100))
let imposibleCase = Drink?(alcohol: .wine(.red), volume: .portion(30))
print(validCase)
print(imposibleCase)
  • Related