Swift Dynamic Variable Name depend on value

后端 未结 2 1775
心在旅途
心在旅途 2021-01-23 07:29

I had a list of variable name like this (BankHelper.swift):

static var va_1_atm =
\"\"\"
  1. Silahkan kunjungi ATM terdekat
相关标签:
2条回答
  • 2021-01-23 07:38

    Why 3 different variables

    static var va_1_atm
    static var va_2_atm
    static var va_3_atm
    

    when we can use array here and use the value of ReservationModel.bankAccount to get to the index of the array to get appropriate text.

    However if you want to do it in this way

    What can be done is, store all your variables in an array. The indexes for the variables you store in that array will correspond to the index values you're trying to include in the variable name.

    var atms = [typeOfAtmVarHere]()
    

    Then keep on adding in this array whenever you have any of static var va_1_atm matching the index based on va_1_atm in var.

    Lastly iterate through it and get it from the array on basis of ReservationModel.bankAccount as index

    0 讨论(0)
  • 2021-01-23 07:59

    Solution with Array

    With continuous integer values, you can make them indexes of an Array.

    class BankHelper {
        static var va_1_atm = "<a>les gorilles</a>"
        static var va_2_atm = "<div>in comedy</div>"
        static var va_3_atm = "<marquee>die sad</marquee>"
        
        fileprivate static var all = [va_1_atm, va_2_atm, va_3_atm]
        
        static func string(for bankAccount: Int) -> String? {
            return all[bankAccount - 1]
        }
    }
    

    Usage:

    cell.bankDesc.text = BankHelper.string(for: ReservationModel.bankAccount)!.htmlToString
    

    Solution with Dictionary

    With discontinuous values, you can make them keys of a Dictionary.

    class BankHelper {
        static var va_1_atm = "<a>les gorilles</a>"
        static var va_2_atm = "<div>in comedy</div>"
        static var va_3_atm = "<marquee>die sad</marquee>"
        
        fileprivate static var all = [1: va_1_atm, 2: va_2_atm, 3: va_3_atm]
        
        static func string(for bankAccount: Int) -> String? {
            return all[bankAccount]
        }
    }
    

    Usage:

    cell.bankDesc.text = BankHelper.string(for: ReservationModel.bankAccount)!.htmlToString
    

    Solution with Mirror

    Alternatively, you may use mirroring to retrieve an instance variable by name (which means that your variables can't be static for this to work), but you may consider a singleton pattern.

    class BankHelper {
        var va_1_atm = "<a>les gorilles</a>"
        var va_2_atm = "<div>in comedy</div>"
        var va_3_atm = "<marquee>die sad</marquee>"
        
        func string(for bankAccount: Int) -> String? {
            return Mirror(reflecting: self).descendant("va_\(bankAccount)_atm") as? String
        }
    }
    

    Usage:

    cell.bankDesc.text = BankHelper().string(for: ReservationModel.bankAccount)!.htmlToString
    

    Solution with NSObject

    You need arbitrary access to static variables? Use a layer of Objective-C by conforming to NSObject.

    class BankHelper: NSObject {
        @objc static var va_1_atm = "<a>les gorilles</a>"
        @objc static var va_2_atm = "<div>in comedy</div>"
        @objc static var va_3_atm = "<marquee>die sad</marquee>"
        
        static func string(for bankAccount: Int) -> String? {
            return value(forKey: "va_\(bankAccount)_atm") as? String
        }
    }
    

    Usage:

    cell.bankDesc.text = BankHelper.string(for: ReservationModel.bankAccount)!.htmlToString
    
    0 讨论(0)
提交回复
热议问题