如何初始化此Swift枚举 - 或者我应该使用它?

时间:2017-01-31 09:03:59

标签: ios arrays swift enums initialization

我想通过读取包含八个参数值的文本文件来初始化Swift中的参数列表,并按照从文件中读取的相同顺序保留参数。一旦初始化参数值不改变。理想情况下,应该有效地访问它们 - 并且对于任何维护代码的人来说尤其明确。

在Playground示例(以下)中,参数值存储在Array Arrays Strings中。然后将来自Enumeration的原始值用作一组索引以解决Array。最初我想过使用Enumeration将参数值存储为要使用参数名称或数字索引进行寻址的原始值。但是从this answer我看不到将参数值直接分配给成员名称的方法。事实上,我开始怀疑我是否应该使用枚举。

有人能提供更好的方法吗?

(这可能听起来很幼稚,因为我之前从未使用过Swift Enumeration

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"

2 个答案:

答案 0 :(得分:3)

使用MapParameters枚举来明确sourceArray每个索引的含义(使其更具可读性)是合法的,但是,它应该是有效的 iff sourceArray.count等于8且其排序始终相同。

您可以将其用作:

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 firstMIDKeyFromSource = sourceArray[MapParameters.firstMIDIKey.rawValue] // 0
let middleKeyFromSource = sourceArray[MapParameters.middleKey.rawValue] // 60

事实上,我发现上面的代码片段代码气味不好;应该更加优化替代,例如:将这些值作为字典接收,或者如果 - 某些 - 您必须将它们作为数组获取,您可能需要来图     它们通过一个简单的模型,类似于:

struct MappedParameters {
    // you might want to give them initial values
    // or you can make them as optionals
    var midiChannel = ""
    var sizeOfRepeatingMapPattern = ""
    var firstMIDIKey = ""
    var lastMIDIKey = ""
    var middleKey = ""
    var referenceMIDIKey = ""
    var referenceFrequency = ""
    var keysPerFormalOctave = ""

    init(sourceArray: [String]) {
        if sourceArray.count  != 8 {
            fatalError("ERROR: sourceArray.count is NOT 8")
        }

        midiChannel = sourceArray[0]
        sizeOfRepeatingMapPattern = sourceArray[1]
        firstMIDIKey = sourceArray[2]
        lastMIDIKey = sourceArray[3]
        middleKey = sourceArray[4]
        referenceMIDIKey = sourceArray[5]
        referenceFrequency = sourceArray[6]
        keysPerFormalOctave = sourceArray[7]
    }
}

用法:

您应该在代码中的某处实现:

let sourceArray = ["5", "12", "0", "127", "60", "69", "440.0", "12"]
let sourceMapper = MappedParameters(sourceArray: sourceArray)

简单地说,使用sourceMapper作为:

print(sourceMapper.firstMIDIKey) // 0
print(sourceMapper.middleKey) // 60

希望这会有所帮助。

答案 1 :(得分:1)

@Ahmad F的答案可能是使用静态成员

<xs:key name="CompositeKeyAnalysis">
    <xs:selector xpath="MasterFiles/AnalysisTypeTable/AnalysisTypeTableEntry"/>
    <xs:field xpath="AnalysisID"/>
    <xs:field xpath="AnalysisType"/>
</xs:key>