King
King

Reputation: 2025

getting the value of a selected key in a dictionary 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])")
}

Upvotes: 2

Views: 1771

Answers (5)

pbodsk
pbodsk

Reputation: 6876

If you need both the key and the value you can use the forEach method of a dictionary (documented here)

This call:

downloadedPhotosData().forEach { (key, value) in
    print("Key is: \(key) - value is: \(value)")
}

Gives me this output:

Key is: XXX1 - value is: ["code": "YYY1"]

Key is: XXX4 - value is: ["code": "YYY4"]

Key is: XXX2 - value is: ["code": "YYY2"]

Key is: XXX3 - value is: ["code": "YYY3"]

Update

So in your updated question you would like to use the inner value of the values in your dictionary.

You can loop through the values of the dictionary and then try to unwrap the value of the inner dictionary.

This should do the trick:

for innerDict in dict.values {
    if let value = innerDict["code"] {
        print("value is: \(value)")
    }
}

Gives me:

value is: YYY1

value is: YYY2

value is: YYY4

value is: YYY3

Hope that helps you.

Upvotes: 2

AppyMike
AppyMike

Reputation: 2064

I've just done this in a playground, should be enough to solve your issue

var dict : [String : [String:String]] = [
"XXX1": [
    "code": "YYY1",
],
"XXX2": [
    "code": "YYY2",
],
"XXX3": [
    "code": "YYY3",
],
"XXX4": [
    "code": "YYY4",
]
]

dict.forEach { (x) in
    print(x.key)
    print(x.value["code"]!)
}

EDIT: Basically, something like this

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

Then in your action sheet

downloadPhotosData().forEach { (x) in

    let action = UIAlertAction(title: x.key, style: .default) { action in
            self.operatorLabel.text = action.title
            print(x.value["code"]!)
            ........
}

Upvotes: 2

Joakim Danielson
Joakim Danielson

Reputation: 52013

In the loop you can do

 downloadPhotosData().keys.forEach( {
    if let code = downloadPhotosData()[$0] as? [String:String]
        print(code["code"])
    }
 })

Upvotes: 2

Kamran
Kamran

Reputation: 15258

You can get the code as below,

if let codeDict = self.downloadPhotosData()[action.title!] as? [String: String] {
     print(codeDict["code"]!)
}

Upvotes: 2

Mohmmad S
Mohmmad S

Reputation: 5088

This could give you an idea

for obj in downloadPhotosData() {
    let mainKey = obj.key // the main Key which is XXXX
    let mainValue = obj.value as! [String: String] //which is dictionary
    let subKey  = mainValue.keys
    let subValues = mainValue.values
    // Out put for First obj
    print(mainKey) // XXX1
    print(mainValue) // ["code": "YYY1"]
    print(subKey) // ["code"]
    print(subValues) // [YYY1]
}

Upvotes: 2

Related Questions