Swift - How to get the last key of a dictionary


This question already has an answer here:

  • How to get last entered value in a Dictionary in Swift? 3 answers

I'm working with a dictionary and I would like to know how to get the last value.

I got the first value by using myDictionary.keys.first and I tried to get the last value by doing myDictionary.keys.last but it's not working.

Thank you for your help.

You cannot access first of last value of dictionary, because Dictionary does not have a specific order, unlike Array. Calling myDictionary.keys will return an array, but you have no guarantee the order of that array will not change when you add/remove elements from dictionary.