Articles, podcasts and news about Swift development, by John Sundell.

Using key paths to create convenience APIs

Published on 16 Mar 2019
Basics article available: Map, FlatMap and CompactMap

Swift’s key paths can let us build APIs that are both really powerful, but also look really nice and clean at the call site. Here we have an extension that lets us easily group any Sequence based on an Element key path:

// This extension will let us group any Sequence (such as an
// Array or a Set), based on a given key path.
extension Sequence {
    func grouped<T: Hashable>(by keyPath: KeyPath<Element, T>) -> [T: [Element]] {
        // Using key path subscripting syntax, we can dynamically
        // access a member of a type based on a key path.
        return .init(grouping: self, by: { $0[keyPath: keyPath] })

func scan(_ string: String, using matchers: [Matcher]) {
    // The result is that our call sites become really clean, since
    // we can simply use a key path literal to group any sequence.
    let matchersByPattern = (
        start: matchers.grouped(by: \.pattern.start),
        end: matchers.grouped(by: \.pattern.end)


Support Swift by Sundell by checking out this sponsor:


Emerge: Continuously monitor and reduce your app’s size. Emerge’s easy to use plugins for GitHub and fastlane will automatically scan your app’s binary and provide you with simple, actionable suggestions on how to make it smaller and, in turn, faster for your users to download. Set up a demo now!