Swiftpack.co - Package - lukaskubanek/OrderedDictionary


Swift 4.2+ Swift Package Manager Carthage License: MIT Twitter: @lukaskubanek

OrderedDictionary is a lightweight implementation of an ordered dictionary data structure in Swift.

The OrderedDictionary struct is a generic collection that combines the features of Dictionary and Array data structures from the Swift standard library. Like Dictionary, it stores key-value pairs with each key being unique and maps each key to an associated value. Like Array, it stores those pairs sorted and accessible by a zero-based integer index.

OrderedDictionary provides similar APIs to collections in the Swift standard library like accessing contents by keys or indices, inserting and removing elements, iterating, sorting, filtering, etc.

Internally, OrderedDictionary uses a backing storage composed of a Dictionary for storing the key-value pairs and an Array for managing the ordered keys. This architecture makes it not the most pefromant implementation possible, but it gets its job done while reusing most functionality from the Swift standard library.


  • Swift 4.2+
  • Xcode 10.0+
  • iOS 8.0+ / macOS 10.10+

For support of Swift 4.0 and 4.1 please refer to version 2.x of this library. The Xcode and OS requirements apply only when the library is integrated as a framework or via the Xcode project.


Swift Package Manager

To install OrderedDictionary using the Swift Package Manager, add it as a dependency into your Package.swift file:

let package = Package(
    dependencies: [
        .package(url: "https://github.com/lukaskubanek/OrderedDictionary.git", from: "3.0.1")


To install OrderedDictionary using Carthage, add it as a dependency into your Cartfile:

github "lukaskubanek/LoremSwiftum"

Then drag either the OrderedDictionary.xcodeproj or the OrderedDictionary.framework into your Xcode project/workspace and link your target against the OrderedDictionary.framework. Make sure that the framework gets copied to your application bundle.

Git Submodules

You can also install OrderedDictionary via Git submodules and integrate the project OrderedDictionary.xcodeproj from the submodule directly into your Xcode workspace.

⚠️ Note About CocoaPods

Although there has been a high demand for CocoaPods support, this distribution method won't be officially supported by this library. If you really want to integrate this library via CocoaPods, you can create and maintain a custom podspec (see the last section of this post).

Usage & Docs

For the usage of this library please refer to the example playground. For documentation please refer to the documentation comments.


Stars: 148


Used By

Total: 0


3.0.1: Xcode Project Fix - 2020-05-28 09:17:08

Re-added marketing version to the Xcode project as it’s used in a required key within the Info.plist (#62).

3.0.0: Swift <4.2 Support Drop & CI Setup - 2020-05-22 07:14:33

  • Dropped support for Swift <4.2. (To use OrderedDictionary with Swift 4.0 or 4.1, please refer to the 2.x versions.)
  • Configured GitHub actions for CI but still kept Travis for older build configurations (Swift 4.2).

2.4.0: Capacity Reservation Support - 2020-03-02 11:04:53

  • Added support for capacity reservation (capacity, init(minimumCapacity:), reserveCapacity(_:)). Kudos to @GalCohen.
  • init(values:uniquelyKeyedBy:) now takes a throwing closure.

2.3.0: Mapping Values & Revamped Initializers - 2019-07-30 13:41:47

  • Introduced mapValues(_:) & compactMapValues(_:)
  • Renamed initializers to better convey their semantics
    • init(_:)init(uniqueKeysWithValues:)
    • init(values:keyedBy:)init(values:uniquelyKeyedBy:)
    • Added shims for the deprecated versions
  • Fixed warnings for Swift 5.0

2.2.2: Fixed Version Numbers 🙈 - 2019-04-29 11:57:36

In version 2.2.1 I accidentally forgot to bump the version numbers. This is now fixed in this further patch release.

2.2.1: Package Definition Fix - 2019-04-29 11:16:53

This patch should address the issue with package definition format reported in #51.

2.2.0: Support for Swift 5.0 - 2019-04-04 10:31:16

  • Added support for Swift 5.0 by making changes necessary for Xcode 10.2
  • This version should still work with Swift 4.0 & 4.2
  • Fixed an issue with reset flag for marking the code as safe for app extensions [#10, #50] (thanks to @graphiclife)

2.1.0: SPM, Optimizations & Additions - 2018-07-10 13:20:39

  • Added support for Swift Package Manager (thanks to @jnordberg)
  • Optimized lookup performace (thanks to @maicki)
  • Introduced Dictionary.sorted(by:)
  • Introduced popFirst(), popLast(), removeFirst(), removeLast() methods
  • Annotated sort methods with throws/rethrows keywords
  • Added note about no support for CocoaPods

2.0.0: Support for Swift 4.1 - 2018-05-22 14:02:13

  • Support for Swift 4.0 is still maintained
  • OrderedDictionary now conditionally conforms to Equatable and Codable
  • Used custom types for keys and values collections
  • Simplified the OrderedDictionarySlice type

1.1.0: API Enhancements - 2018-03-27 13:37:29

  • Improved methods for insertion checks (aka OrderedDictionary.canInsert(...))
  • Added an initializer from regular Dictionary
  • Added support for moving key-value pairs to a new index

1.0.0: One Point O - 2017-09-25 09:03:23

This is the official release of OrderedDictionary which supports Xcode 9 and Swift 4. 🎉

1.0.0 β3: Codable & New Initializers - 2017-08-12 15:27:01

  • Supports Xcode 9 β5
  • Added initial conformance to Swift's Codable
  • Added two new initializers for instantiating an OrderedDictionary from a sequence of values
  • Exposed unorderedDictionary property
  • Removed migration code
  • Version 1.0.0 will be released when the final version of Swift 4 arrives

1.0.0 β2: Support for Swift 4.0 - 2017-07-18 14:14:30

This beta version supports Xcode 9 (β3) and Swift 4.

1.0.0 β1: Swift 3 API Revamp - 2017-01-12 21:38:11

  • Added support for Swift 3
  • Completely reworked the API and aligned it with the Swift Design Guidelines
  • OrderedDictionary now conforms to BidirectionalCollection
  • Removed mutation based on random access
  • Handled the edge case of storing key-value pairs by index which contain non-unique keys (added methods for safe checking and triggering runtime errors if not handled)
  • Introduced OrderedDictionarySlice
  • Added migration support for renaming some methods using Xcode fixits when coming from earlier versions
  • Improved tests
  • Added code documentation
  • Improved Travis setup

0.8.0: Support for Swift 2.3 - 2016-09-24 19:21:00

0.7.0: Various Enhancements - 2016-05-05 16:59:03

  • OrderedDictionary now conforms to DictionaryLiteralConvertible (#4)
  • Used the default generator which comes from the CollectionType
  • Added method valueForKey(_:) which can be preferred for dictionaries with key type Int, since the usage of subscript might be ambiguous in that case (#13)
  • (Possibly) made the code safe to be used with app extensions (#10)
  • Improved example playground and added comments

0.6.0: Support for Swift 2.2 - 2016-03-27 15:00:02

This release fully supports Swift 2.2 and Xcode 7.3

0.5.0: iOS Deployment Target Fix - 2016-01-06 22:53:00

The iOS deployment target set to 8.0 (thanks to @mbrgm)

0.4.0: Sorting - 2015-12-06 13:53:56

  • Added convenience properties for accessing ordered keys and values
  • Added convenience method for inserting new elements
  • Added support for sorting (thanks to @aliHafizji)
  • Rewritten the tests to use XCTest only and removed the dependency on Nimble which caused problems on Travis

0.3.0: Index-Based Insertion - 2015-09-11 08:27:12

  • Added support for index-based insertion using insertElement(:, atIndex:)
  • Improved fatal error messages for invalid indexes

0.2.0: Little Enhancements - 2015-09-10 17:14:54

  • Added support for initializing the ordered dictionary with pairs
  • Added support for the == operator
  • Some other minor enhancements

0.1.0: Initial Release - 2015-08-30 09:12:58

  • The initial release provides basic functionality
  • Support for iOS and OS X
  • Initial unit tests
  • The API documentation is lacking and will be improved later