0

Plan to use a string value to for referencing which Object I want to update but uncertain how to accomplish this. Combining string from a few different user selected sources. To many possibilities to use if/case statements. See below code, [ref] is where I'm trying to use string. Thanks in advance.

class Equipment {
        var eType: String = "default"
        var name: String = "default"
        var a: Double = 1
        var b: Double = 0
        // ...
        var z: Int = 0

        init(eType: String, name: String){self.eType = eType; self.name = name
        }
    }
    var d1010 = Equipment(eType: "Panel X", name: "L1 Panel X")
    // ...
    var d1289 = Equipment(eType: "Panel X", name: "L2 Panel 39")
    // ...
    var d1999 = Equipment(eType: "Panel X", name: "L2 Panel 99")

    var deviceIDtype: Character = "d" // some input value from button press
    var deviceIDsection: String = "12" // some input value from button press
    var deviceID: String = "89" // some input value from button press

    func devName(dIDt:Character, dIDs: String, dID: String)  -> String {
        var combine: String = String(dIDt) + (dIDs) + (dID)
        return (combine)
    }

    let ref = devName(deviceIDtype, dIDs: deviceIDsection, dID: deviceID)
    // ref = d1289

    // d1289.etype = "some value"
    // d1289.name = ...
    // need something below to work like above using ref to get values to right object
    [ref].etype = "some val"
    [ref].name = "some val"
    [ref].a = 1.1
    [ref].b = 2.2
    // ...
    [ref].z = 24

1 Answer 1

1
var equipments: [String: Equipment] = [:]
equipments["d1010"] = Equipment(eType: "Panel X", name: "L1 Panel X")
equipments["d1289"] = Equipment(eType: "Panel X", name: "L2 Panel 39")
equipments["d1999"] = Equipment(eType: "Panel X", name: "L2 Panel 99")

...

equipments[ref]?.etype = "some value"
equipments[ref]?.name = "name"
...

?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.