Any one have an idea how to make a reference between an ENUM and a TEXTFILE?

I have a game program (in Swift) that uses a textfile to define each game (currently 90 of them). The textfile is NOT XML or JSON, just key/value pairs for the most part…

Example of Text File

GameID=01023
Help  =01023
TITLE=Monte Carlo
VALID

Decks=1,*,*
ruleGAME             = 209
winRULE              = 803
rulePickupStock      = 6
rulePickupWaste      = 0
rulePickupTableau    = 4
rulePickupFoundation = 0
goodMove             = 1001
emptySTOCK           = 302

where the enum (in the code of course) are like

public enum ruleGAME : Int {
    case noRULE          = -1
    case kBlank          = 0
    case diffNOWRAP      = 206
    case diffWRAP        = 207
    case rankORdiff      = 208
    case rowORcol        = 209

The problem is the value 209 which is ‘ruleGame’ in the textfile, is ‘rowORcol’ in the enum
when creating the textfiles I have to always refer to the code to get the correct values. What I would like to do is be able to leave the ENUM as it is, but change the text file

From
ruleGAME = 209
to
ruleGAME = rowORCol

the only way I can think to do this is create a dictionary, but that seems excessive, there should be a way to utilize the existing Enum

The entry

ruleGAME = 209

is a symbolic value = literal

but the entry

ruleGAME = rowORCol

is symbolic value = symbolic value

so the right hand symbolic value requires some kind of secondary lookup to know what it maps to

at least thats how I see that

so its not so much that you need a dictionary but a second lookup when you encounter a NON- numeric value

I think

this I understand… something to convert the symbolic to a literal… a reverse of the enum as it were

AI says “use a dictionary”

What about using an enum of type String?

public enum ruleGAME: String {
  case noRULE = "noRULE"
  case kBlank = "kBlank"
  case diffNOWRAP = "diffNOWRAP"
  case diffWRAP = "diffWRAP"
  case rankORdiff = "rankORdiff"
  case rowORcol = "rowORcol"
}

Then use the rawValue property to make comparisons or assignments?

if thisGameRuleValueAsString == ruleGAME.rowORcol.rawValue {
  // do something special
}

Just a thought.

Why not just iterate over the enum and find the one that matches then take its value and use that ?

Just that somehow you need to turn the text value “ruleOrGame” into 209

Unless swift enums have a way to look up the Enums value by its symbolc value ?
ie

int value = ruleOrGame.ByName("rowOrCol")

?

EDIT : seems that runtime access to the symbolic names in the enum (reflection) doesnt exist :frowning:

bummer

that actually could work… the reason it is numbers is that it all started when I ported an ancient DOS game years and years ago… it has been updated many time and this version is the first to actually be in the App Store.

2 Likes