Digital Tip #8: Getting all keys for a concrete value in a dictionary - Digital Leaves
post-template-default,single,single-post,postid-1920,single-format-standard,qode-social-login-1.0,qode-restaurant-1.0,ajax_fade,page_not_loaded,,select-theme-ver-4.1,wpb-js-composer js-comp-ver-5.2,vc_responsive

Digital Tip #8: Getting all keys for a concrete value in a dictionary

The Digital Tip of the week is a very useful extension that I use in my dictionaries to get the keys that match a concrete key. The inverse operation is pretty easy for a dictionary, just calling dictionary[key], but as the same value may be assigned to multiple, different keys, I sometimes find it really handy to be able to quickly get all keys in a dictionary that are mapped to a concrete value. This is the extension I use:

As you may recall from Sequence Hacking in Swift, Dictionaries are sequences, and as such, we can apply all the Sequence operations (such as map, flatMap, filter, etc). I hope this tip is useful to you too. Have anything to add? Do you have a favorite extension that you would like to share with us? Please, don’t hesitate to do so in the comments below!

No Comments

Post a Comment

Before you continue...

Hi there! I created the Digital Tips List, the newsletter for Swift and iOS developers, to share my knowledge with you.

It features exclusive weekly tutorials on Swift and iOS development, Swift code snippets and the Digital Tip of the week.

Besides, If you join the list you'll receive the eBook: "Your First iOS App", that will teach you how to build your first iOS App in less than an hour with no prior Swift or iOS knowledge.

I hate spam, and I promise I'll keep your email address safe.