I want to initialise a list of parameters in Swift by reading text files containing eight parameter values and preserve the parameters in the same order they are read from file. Once initialised parameter values do not change. Ideally they should be accessed efficiently - and be especially clear for anyone maintaining code further down the track.
In the Playground example (below) parameter values are stored in an Array
of Arrays
of Strings
. Raw values from the Enumeration
are then used as a set of indices to address the Array
. Initially I thought of using Enumeration
to store parameter values as raw values to be addressed using either the parameter name or a numerical index. But from this answer I cannot see a way to assign parameter values directly to member names. In fact I'm starting to doubt whether I should be using an enumeration at all.
Can anyone offer a better way to do this ?
(This probably sounds naive as I've never used a Swift Enumeration
before).
enum MapParameters: Int {
case midiChannel = 0,
sizeOfRepeatingMapPattern = 1,
firstMIDIKey = 2,
lastMIDIKey = 3,
middleKey = 4,
referenceMIDIKey = 5,
referenceFrequency = 6,
keysPerFormalOctave = 7
}
let sourceArray = ["5", "12", "0", "127", "60", "69", "440.0", "12"]
let mapIndex = MapParameters.referenceFrequency //referenceFrequency
let i = mapIndex.rawValue // 6
let parameterValue = sourceArray[i] // "440.0"