Swift Dynamic Variable Name depend on value

后端 未结 2 1776
心在旅途
心在旅途 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

提交回复
热议问题