18

I want to use values from a dictionary in my UITableViewCells. Can I get those from a dictionary by using indexPath.row for the value and indexPath.section for the key?

Vincent
  • 1,645
  • 3
  • 14
  • 22

4 Answers4

65

you can get the keys array and get the key at indexPath.section like this:

Array(yourDictionary.keys)[indexPath.section]

and you can get the value at indexPath.row from the values array like this:

Array(yourDictionary.values)[indexPath.row]

Edit:

if you want to get the values of a specific section key you should write:

let key = Array(yourDictionary.keys)[indexPath.section]
let array = yourDictionary[key]
let value = array[indexPath.row]
Firas
  • 1,742
  • 15
  • 25
  • Thanks! I forgot to mention, sorry, that the dictionary I'm talking about looks like this: [String: [AnyObject]]() So I have multiple objects per key. Is it possible to have an array for the values of a specific key? – Vincent Aug 02 '15 at 20:19
  • Beware, though, that the order of keys returned by dictionary.keys is not defined, and can change from call to call. So you need to save the array of keys as an instance variable, and you should really persist it to a file and read that (at least until the contents of the dictionary change. ) – Duncan C Aug 02 '15 at 22:07
  • It seems like the array property is no longer available. However, this works: `let key = [String](yourDictionary.keys)[indexPath.section]` – Ryan H. Jan 05 '16 at 19:01
  • This is a bad idea. – Duncan C Dec 13 '16 at 12:21
  • @DuncanC The array of keys will always have the same order, you can test it: var dictionary = ["1":1, "2":2, "3":3, "5":5] print(Array(dictionary.keys)) print(Array(dictionary.keys)) print(Array(dictionary.keys)) print(Array(dictionary.keys)) print(Array(dictionary.keys)) print(Array(dictionary.keys)) print(Array(dictionary.keys)) dictionary["13"] = 33 print(Array(dictionary.keys)) print(Array(dictionary.keys)) print(Array(dictionary.keys)) print(Array(dictionary.keys)) – Firas Dec 14 '16 at 07:48
  • @Firas, that is wrong. Dictionary keys often come in the same order but it is not guaranteed. Relying on the order of keys in an unordered collection is a very bad idea. – Duncan C Dec 14 '16 at 11:47
  • To quote SwiftDoc.org: "The order of key-value pairs in a dictionary is stable between mutations but is otherwise unpredictable." – Duncan C Dec 14 '16 at 11:51
  • Or see this thread: http://stackoverflow.com/questions/29601394/swift-stored-values-order-is-completely-changed-in-dictionary – Duncan C Dec 14 '16 at 12:04
9

Dictionaries are inherently unordered. If you use dictionary.keys, you can get an array of keys and use that, as @Firas says in his/her answer, but there's no guarantee that next time you fetch an array of keys they will be in the same order.

Another option would be to use Int as the key type, then use the indexPath.row as the key.

It's really better to use an array as a data source for a table view.

If you want to store the values for a sectioned table view in a dictionary you should use an outer section array, which contains an inner row array, which contains a dictionary of values for the cell.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
4

You can use:

Array(yourDictionary)[indexPath.row].key // or .value
Mahmut K.
  • 707
  • 1
  • 8
  • 20
0

Here is my small trick to access Dictionary by index. Just wrap dictionary!

var dict = [String: [Int]]()

dict.updateValue([1, 2, 3], forKey: "firstKey")
dict.updateValue([3, 4, 5], forKey: "secondKey")

var keyIndex = ["firstKey": "firstKey", "secondKey": "secondKey"]
var arr = [[String: [Int]]]()

for (key, value) in dict {
    arr.append([keyIndex[key]!: value])
}

print(arr[0]) // ["firstKey": [1, 2, 3]]
Changnam Hong
  • 1,669
  • 18
  • 29