How do you make a variable from a string?

后端 未结 2 794
天命终不由人
天命终不由人 2021-01-27 05:49

I want to be able to convert a String into a variable name and then use it to call a variable in another Swift file.

//ViewController.swift
var hireTypes = [\"sc         


        
2条回答
  •  不要未来只要你来
    2021-01-27 06:37

    A better option is to use the string as a key into a dictionary to get the values.

    E.g.

    static var dataDictionary = [
    "school" : [
        "name": "School",
        "warningMessageOne": "Please check system value",
        "warningMessageThree": "May or June",
        "warningMessageTwo": "Check hire fits in with morning and afternoon school runs"
     ],
    "council" : [
        "name": "Council",
        "warningMessageOne": "Please check system value",
        "warningMessageThree": "Aug or June",
        "warningMessageTwo": "Check hire fits in with morning and afternoon school runs"
     ],
     .....
    ]
    

    Then access the data as

    var data = FullData.dataDictionary[variableName]
    

提交回复
热议问题