0

Is there a way to define name of variable from value(array)? I need it for automatically creating arrays, but I don't want type name manually, rather have it chosen from an array.

dataEmail = ["1", "POS101582781", "2018.10.26 10:02", "Buy", "1", "VMID", "29.435", "", "", "", "30.42", "0.99", "30.42\n2", "POS104121282", "2018.12.10 10:00", "Buy", "1", "PSN", "1940", "", "", "", "2440", "5", "24.4\n3", "POS101582480", "2018.10.26 10:00", "Buy", "2", "HL", "1796.5", "", "", "", "1730", "-1.33", "34.6\n4", "POS101582499", "2018.10.26 10:00", "Buy", "8", "INVP", "473.6", "", "", "", "493.4", "1.58", "39.47\n5", "POS102186690", "2018.11.06 10:00", "Buy", "40", "VOD", "153.63", "", "", "", "134.38", "-7.7", "53.75\n6", "POS105530053", "2019.01.04 10:00", "Buy", "5", "MKS", "253.4", "", "", "", "272.5", "0.96", "13.63\n7", "POS102224924", "2018.11.06 17:52", "Buy", "14", "EVR", "488.15", "", "", "", "563.4", "10.54", "78.88\n8", "POS102087914", "2018.11.02 17:32", "Buy", "59", "DC", "164.112", "", "", "", "133.2", "-18.24", "78.59\n9", "POS101582520", "2018.10.26 10:00", "Buy", "20", "BT", "241.4", "", "", "", "214.9", "-5.3", "42.98\n10", "POS101582491", "2018.10.26 10:00", "Buy", "101", "LLOY", "55.5975", "", "", "", "63.58", "8.06", "64.22\n11", "POS101582545", "2018.10.26 10:00", "Buy", "26", "AV", "413.662", "", "", "", "423.2", "2.48", "110.03\n12", "POS104121258", "2018.12.10 10:00", "Buy", "10", "PTEC", "389.6", "", "", "", "420.9", "3.13", "42.09\n13", "POS104962193", "2018.12.24 10:40", "Buy", "11", "RMG", "286.009", "", "", "", "283.4", "-0.29", "31.17\n14", "POS105487268", "2019.01.03 18:29", "Buy", "22", "CREI", "115.0909", "", "", "", "113.6", "-0.33", "24.99\n15", "POS108707475", "2019.02.14 10:00", "Buy", "4", "IMB", "2640", "", "", "", "2508.5", "-5.26", "100.34\n16", "POS102853078", "2018.11.16 11:45", "Buy", "3", "HMSO", "418.067", "", "", "", "377.9", "-1.21", "11.34\n17", "POS103378446", "2018.11.27 10:12", "Buy", "60", "CNCT", "37.3667", "", "", "", "38.65", "0.77", "23.19"]


let arrayName = ["VMID", "PSN", "HL", "INVP", "VOD", "MKS", "EVR", "DC", "BT", "LLOY", "AV", "PTEC", "RMG", "CREI", "IMB", "HMSO", "CNCT"]


func createStockSet (){
    var indexNumber = 1
    for n in 0...16 {
        var  arrayName[0] = StockClass(transactionName: dataEmail[indexNumber], date: dataEmail[indexNumber+1], type: dataEmail[indexNumber+2], amount: dataEmail[indexNumber+3], stockTinker: dataEmail[indexNumber+4], boughtPrice: dataEmail [indexNumber+5], nowPrice: dataEmail[indexNumber+6], floatingResult: dataEmail[indexNumber+9], margin: dataEmail[indexNumber+10])
        indexNumber + 3
    }}
7
  • 4
    Possible duplicate of Swift Dynamic Variable Name depend on value, Create a variable in Swift with dynamic name, Swift converting a string into a variable name, etc. Commented Mar 18, 2019 at 16:46
  • 3
    In most languages, you'd use a dictionary/map. Dynamically creating variables is really never the correct way to accomplish something unless that's your only option. Commented Mar 18, 2019 at 16:47
  • Please explain why you want the variable name to be based on a string from an array. What is your actual goal here? How do you intend to make further use of these dynamically named variables? Once we know why, we can offer proper solutions. Commented Mar 18, 2019 at 16:55
  • could anybody write quick example please, I keep looking at that recommended articles, but cant see how I would implement it, they all recommend using arrays instead, but I do have array, just dont know syntax to use it... thank you Commented Mar 18, 2019 at 18:08
  • Your updated question still doesn't explain why you need dynamic variable names nor does it explain how you will use them outside of the for loop. Commented Mar 19, 2019 at 2:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.