how to loop through dictionary keys and compare to my Integer in swift -
i have quick question how loop through dictionary key , compare key integer, print out corresponding value if key exists.
for example, code
for(var j = 0; j <= somedictionary.count; j++) { if ( someinteger == somedictionary.keys[j] ) { somedictioanrysvalue = somedictionary.values[j] println(somedictioanrysvalue) } }
is there method in dictionary want already?
if understand you're trying correctly, you're looking use subscript - no for
loops required.
let dict = [1: "a", 3: "b", 5: "c"] let myint = 3 if let value = dict[myint] { print(value) } // prints: b
if value doesn't exist key supplied, nil
returned , code inside if let
won't executed.
if you're unfamiliar i'd recommend take @ the swift programming language.
Comments
Post a Comment