Home > Blockchain >  How to find key from Dictionary having value as dictionary in Swift?
How to find key from Dictionary having value as dictionary in Swift?

Time:03-02

I am having dictionary whose value is another dictionary

for ex. ["1": ["1623699578": 1.08], "2": ["1216047930": 6.81]]

I want to take out key of insider dictionary for ex. 1623699578.

suppose my condition is if selectTag == "1" get 1623699578, if selectTag == "2" get 1216047930,

is there any way to achieve this?

CodePudding user response:

Assuming your dictionary will be of type [String: [String: Double]], you can try below code.

let mainDict: [String: [String: Double]] = ["1": ["1623699578": 1.08], "2": ["1216047930": 6.81]]
//Key for which you want the value like 1,2,3...
let key = "1"

if let valueForMainKey = mainDict[key],
   let value = valueForMainKey.keys.first {
    print("value:- \(value)")
}
  • Related