I believe you can use a dictionary. Here are two ways to do the dictionary part.

var someProtocol = [String : Int]()
someProtocol["one"] = 1
someProtocol["two"] = 2

or try this which uses type inference

var someProtocol = [
    "one" : 1,
    "two" : 2
]

as for the for loop

var index: Int
for (e, value) in someProtocol  {
    index = value
}
Answer from Dew Time on Stack Overflow
🌐
Swift Forums
forums.swift.org › using swift
Best way to create a HASH TABLE out of an ARRAY in Swift, please? - Using Swift - Swift Forums
March 1, 2023 - Hey Team, I wrote some working Swift code! (Yay!) My investigation into building and using a HASH TABLE in Swift brought me here : But that looks like a heckin’ of a lot of work and was written six years ago. The approach I took was to use the fact that Dictionary conforms to Hashable and therefore can function as a Hash Table.
🌐
Medium
medium.com › swift-algorithms-extras › understanding-hash-tables-dictionaries-sets-with-swift-4605e905973e
Understanding Hash Tables, Dictionaries & Sets with Swift | by Wayne Bishop | Swift Algorithms Extras | Medium
January 2, 2019 - As a protocol, Hashable ensures all conforming types get correctly indexed with a unique numerical value. Under this model, the required hashValue computed property acts as the hash algorithm. We can also see this functionality in action when working with standard Swift collection types such as Sets:
🌐
Kodeco
kodeco.com › 206-swift-algorithm-club-hash-tables
Swift Algorithm Club: Hash Tables | Kodeco
January 26, 2018 - Learn how to implement a hash table data structure in Swift, in this step-by-step tutorial from the Swift Algorithm Club.
🌐
Apple Developer
developer.apple.com › documentation › swift › sequence › map(_:)
map(_:) | Apple Developer Documentation
Returns an array containing the results of mapping the given closure over the sequence’s elements.
🌐
DEV Community
dev.to › binoy123 › making-your-own-dictionary-in-swift-3ec9
Making Your Own Dictionary(HashMap) in Swift - DEV Community
December 4, 2025 - Swift has a powerful built-in Dictionary, but learning how it works behind the scenes helps you understand data better. This article explains how to build your own Dictionary using three methods: ... _In Java's HashMap, if a linked list in a bucket grows longer than 8 entries, it automatically converts that list into a balanced tree (Red-Black Tree) to improve lookup speed.
Find elsewhere
🌐
Medium
medium.com › journey-of-one-thousand-apps › building-a-hash-data-structure-in-swift-e9b2733d9e20
Building A Hash Data Structure In Swift | by Christopher Webb | Journey Of One Thousand Apps | Medium
January 23, 2018 - In Swift, you may have seen a protocol called Hashable. Anything that conforms to Hashable needs to have a computed integer property called hashValue.
🌐
Andy Ibanez
andyibanez.com › posts › understanding-basic-data-structures-dictionaries-in-depth
Understanding Basic Data Structures in Swift: Dictionaries in Depth - Andy Ibanez
January 27, 2021 - There is nothing in the definition of a dictionary (or a hashmap) that guarantees an order for the keys.
🌐
DhiWise
dhiwise.com › post › navigating-key-value-pairs-with-the-swift-dictionary
Navigating Key-Value Pairs with the Swift Dictionary
July 10, 2024 - Dictionaries in Swift are versatile and powerful data structures that allow for the storage and retrieval of key value pairs. They play a critical role in many programming scenarios where associative mapping of data is needed. If you come from other programming languages, you might be familiar with similar structures called maps, hashmaps...
🌐
Medium
alcivanio.medium.com › how-to-implement-a-hashtable-using-swift-aea632ee75ac
How to implement a HashTable using Swift | by Alcivanio (Swift) | Medium
February 25, 2020 - How to implement a HashTable using Swift I was curious about some data structures, and the hash table is one of them. I was wondering how I could implement one of these guys using Swift. They're …
🌐
Swift.org
docs.swift.org › swift-book › documentation › the-swift-programming-language › collectiontypes
Documentation
This document is made available under a Creative Commons Attribution 4.0 International (CC BY 4.0) License · Swift and the Swift logo are trademarks of Apple Inc
🌐
GitHub
gist.github.com › rhysm94 › 4b231dff0e618180e9001ca3c7da0d8c
HashMap.swift · GitHub
HashMap.swift · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
Cocoa Casts
cocoacasts.com › swift-essentials-1-how-to-use-swift-map-to-transforms-arrays-sets-and-dictionaries
How to Use Swift Map to Transform Arrays, Sets, and ...
It should be no surprise that Swift's map(_:) method can also be used to transform the keys and values of a dictionary. This is a bit odd, though. The key-value pairs are transformed to an array and that isn't a common operation.
🌐
Apple Developer
developer.apple.com › forums › thread › 62637
What is equivalent to Hashmap(of python and java) in swift ...
September 12, 2016 - i am using odoo OpenERP based python api which takes a hash-map in its function but when i calling that from my swift code i passed a dictionary. i tried many thing apart from dictionary what should i do ?
🌐
Swift Forums
forums.swift.org › evolution › discussion
Adding more data structures to the standard library - Discussion - Swift Forums
April 25, 2019 - I'm studying for job interviews for Swift positions by completing code problems. By far the most painful part of this preparation is the dismal state of data structures in Swift. If I were programming in Java (which I r…
🌐
Gitbooks
dennis-xlc.gitbooks.io › swift-algorithms-data-structures › content › chapter13.html
Hash Tables | Swift Algorithms & Data Structures
Here's a hash table data structure written in Swift. At first glance, we see the structure resembles a linked list. In a production environment, our HashNode could represent any combination of items, including custom objects.
🌐
GitHub
github.com › antonio081014 › LeetCode-CodeBase › blob › main › Swift › design-hashmap.swift
LeetCode-CodeBase/Swift/design-hashmap.swift at main · antonio081014/LeetCode-CodeBase
This repo presents all the solution I passed on LeeCode, should be used AS a Reference for study purpose. - LeetCode-CodeBase/Swift/design-hashmap.swift at main · antonio081014/LeetCode-CodeBase
Author   antonio081014