1

Plan to use a string value to for referencing which variable I want to update. Combining string from a few different user selected sources. To many possibilities to use if/case statements. Thanks in advance

var d1000: Int = 0
// ...
var d1289: Int = 0
// ...
var d1999: Int = 0

var deviceIDtype: Character = "d" // button press assigns some value, d used for example
var deviceIDsection: String = "12" // button press assigns some value, 12 used for example
var deviceID: String = "89" // button press assigns some value, 89 used for example

var ref:String = ""

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

ref = devName(deviceIDtype, dIDs: deviceIDsection, dID: deviceID) // ref equals d1289 in this example


// d1289 = 1234 // trying to set this using the ref variable value, failed attempts below
/(ref) = 1234 // set d1289 variable to equal "1234"
"/(ref)" = 1234 // set d1289 variable to equal "1234"
get(ref) = 1234 // set d1289 variable to equal "1234"
get.ref = 1234 // set d1289 variable to equal "1234"
1

2 Answers 2

8

It is possible!!!

let index = 1000
if let d1000 = self.value(forKey: "d\(index)") as? Int {
    // enjoy
}
Sign up to request clarification or add additional context in comments.

2 Comments

How can the result be an Int?, it's got a "d" (string), in the key's value, so can't be an integer, right?
d1000 is the key. i,e. the variable name. Int is the value it contains. it depends on how you declare d1000 in your class(self). check out this example. class Temp { var myVar = "hello"} in this case if i take value(forKey:), i will get String. if let myVarRep = self.value(forKey: "myVar") as? String {}
5

How about using a dictionary, [String : Int]?

This will allow you to achieve what you want - storing values for different keys.

For example, instead of using

var d1000 = 0
var d1289 = 0
var d1999 = 0

You could use

var dictionary: [String : Int] = [
  "d1000" : 0,
  "d1289" : 0,
  "d1999" : 0
]

To store a value in the dictionary, just use

dictionary[key] = value
//for example, setting "d1289" to 1234
dictionary["d1289"] = 1234

and to get the value from the dictionary, use

let value = dictionary[key]
//for example, getting the value of "d1289"
let value = dictionary["d1289"]

So, you could use something like this

//initialize your dictionary
var myDictionary: [String : Int] = [:]

//your key initialization data
var deviceIDtype: Character = "d"
var deviceIDsection: String = "12"
var deviceID: String = "89"
var ref: String = ""

//your code
func devName(/*...*/){/*...*/}
ref = devName(/*...*/)

//set the key ref (fetched from devName) to 1234
myDictionary[ref] = 1234

Just as a side note, you could really clean some of your code

func devName(type: Character, section: String, id: String) -> String{      
    return String(type) + section + id
}

//...

let key = devName(deviceIDtype, section: deviceIDsection, id: deviceID)
let value = 1234
myDictionary[key] = value

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.