Little Snippets #1: Group By in Swift 3

A short implementation of the classic ‘group by’ function in Swift 3 as an Array extension.

Given an array of elements, and a criteria function, grouped(by:) transforms that array into a dictionary with the keys that the criteria function returns.

The criteria function takes an Element and returns its classification. For example, if you want to separate even numbers from odd ones, your criteria closure will return “even”, or “odd”.

I often use this for separating elements by creation date.

I hope this will help you. If you know a better way of doing this, please let me know below, in the comments! Thanks! 🙌

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Fernando Martín Ortiz

Written by

iOS Developer | Interested in mobile development and UX design | https://github.com/fmo91 ||| Contact: ortizfernandomartin@gmail.com

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade