ios - Proper way to get swift dictionary item index -
i have dictionary in app written in swift var cities: [string: string] = ["":"not specified", "ny":"new york", "la":"los angeles", "sf":"san francisco"] i use dictionary build pickerview. when user selects 1 of items - city code saving device memory. when user opens app next time want pickerview show saved city. how should it? you can find index of key in dictionary higher-order functions: let desiredkey = "ny" let pos = x.enumerate().filter { (pair: (index: int, element: (string, string))) -> bool in return pair.element.0 == desiredkey }.map { (pair: (index: int, element: (string, string))) -> int in return pair.index } pos optional(int) containing position of "ny" in current iteration order of dictionary. store value of key somewhere (say, in user defaults), retrieve it, index of key-value pair using code above, , pass selectedrowincomponent: ...