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])")
}
You can get the code as below,
if let codeDict = self.downloadPhotosData()[action.title!] as? [String: String] {
print(codeDict["code"]!)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With