Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the value of a selected key in a dictionary swift

Tags:

ios

swift

I have a set of dictionary that I defined and I am passing the value of the dictionary to an action sheet and I want to be able to access the key of the particular selected key. below is the code I have tried so far

func downloadPhotosData() -> [String: Any] {
        return [
            "XXX1": [
                "code": "YYY1",
            ],
            "XXX2": [
                "code": "YYY2",
            ],
            "XXX3": [
                "code": "YYY3",
            ],
            "XXX4": [
                "code": "YYY4",
            ]
        ]
    }

I am able to pass the key into action sheet as such

 downloadPhotosData().keys.forEach {

            let action = UIAlertAction(title: $0, style: .default) { action in
                self.operatorLabel.text = action.title
........

which displays the key in the Alert Sheet but now I want to be able to print the value of code of the selected key. Any way to do that would be appriciated

I am able to get the index value of the selected action sheet as below but how do I get the "code" value of the selected index

if let index = sheet.actions.index(where: {$0 === action}){

                    print("INDEX \([index])")
}
like image 415
King Avatar asked Dec 06 '25 23:12

King


1 Answers

You can get the code as below,

if let codeDict = self.downloadPhotosData()[action.title!] as? [String: String] {
     print(codeDict["code"]!)
}
like image 78
Kamran Avatar answered Dec 08 '25 11:12

Kamran