Parth Dhorda
Parth Dhorda

Reputation: 722

Change Mapper value of enum in swift

I have an enum and a mapper function inside the enum. The mapper function calls on application launch. Now I want to call the mapper function again and wants to change the value for particular case of enum.

enum AuthenticationToken:Int{

case admin
case customer

static let mapper: [AuthenticationToken: String] = [
    .admin: "\(appDelegate.adminToken)",
    .customer: "\(appDelegate.customerToken)"
]
var string: String {
    return "Bearer " + AuthenticationToken.mapper[self]!
}

}

here I am getting the value from the variable and if the value is changed then I want to update those here as well.

Any help will be appreciated

Upvotes: 0

Views: 39

Answers (1)

a.masri
a.masri

Reputation: 2469

Try this code

enum AuthenticationToken:Int{

    case admin
    case customer

    static var mapper: [AuthenticationToken: String] = [
     .admin: "\(appDelegate.adminToken)",
     .customer: "\(appDelegate.customerToken)"
    ]
    var string: String {
        return "Bearer " + AuthenticationToken.mapper[self]!
    }

}

Call enum

 AuthenticationToken.mapper = [.admin : "Ahmad",.customer: "ali"]
  print(AuthenticationToken.admin.string)

Output

"Bearer Ahmad"

Upvotes: 1

Related Questions