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.
🌐
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.
🌐
Codecademy
codecademy.com › learn › intro-to-algorithms-and-linear-data-structures-in-swift › modules › hash-tables-swift › cheatsheet
Introduction to Algorithms and Linear Data Structures in Swift: Hash Tables Cheatsheet | Codecademy
Hash map data structures use a hash function, which turns a key into an index within an underlying array. The hash function can be used to access an index when inserting a value or retrieving a value from a hash map.
🌐
YouTube
youtube.com › seemu apps
Hashmaps explained with Swift and Dictionaries - YouTube
Hashmaps explained using Swift and how Dictionaries in Swift actually use Hashmaps as the underlying data structure.Check out our iOS Course: http://www.seem...
Published   August 20, 2017
Views   4K
🌐
Kodeco
kodeco.com › 206-swift-algorithm-club-hash-tables
Swift Algorithm Club: Hash Tables | Kodeco
January 26, 2018 - In this tutorial, you’ll learn how to implement a Swift hash table. This data structure was first implemented by Matthijs Hollemans, and is now refactored for tutorial format. As a Swift developer, you’re familiar with the Dictionary structure. If you’re also a Kotlin developer, you would be familiar with the HashMap class.
🌐
GitHub
gist.github.com › rhysm94 › 4b231dff0e618180e9001ca3c7da0d8c
HashMap.swift · GitHub
Save rhysm94/4b231dff0e618180e9001ca3c7da0d8c to your computer and use it in GitHub Desktop. Download ZIP · Raw · HashMap.swift · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
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.
Find elsewhere
🌐
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 ?
🌐
Dev Swiftly
devswiftly.com › home › hashmap in swift
HashMap in Swift - Dev Swiftly
June 26, 2024 - A hash map is a data structure that stores key-value pairs. It uses a hash function to compute an index into an array of buckets, where the key-value pair is stored. Key Characteristics of Hash Maps Operations in a Hash Map HasMap in Swift In Swift, a dictionary is a built-in data structure ...
🌐
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.
🌐
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 - Whether you are a seasoned developer ... dictionaries being used in many places. Also known as hashmaps or hash tables, dictionaries allow us to store key-value mappings, from one object to another....
🌐
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:
🌐
YouTube
youtube.com › lets build that app
Swift: Magical Grid - Hash Map Views Dictionary (Ep 2) - YouTube
Today we figure out how to select the cells that are inside of our grid based on the location of our gesture. First I'll demonstrate a method that isn't all...
Published   April 17, 2017
Views   11K
🌐
Kodeco
kodeco.com › books › swift-cookbook › v1.0 › chapters › 7-use-dictionaries-in-swift
Swift Cookbook, Chapter 7: Use Dictionaries in Swift | Kodeco
Dictionaries, also known as hash maps or associative arrays, are a powerful collection type in Swift for storing key-value pairs. The keys in a dictionary must be unique and can be of any hashable type, such as String or Int.
🌐
Medium
stevenpcurtis.medium.com › implement-a-dictionary-in-swift-5e06052aa120
Implement a Dictionary in Swift. It is not just a HashMap…but how can we… | by Steven Curtis | Medium
May 27, 2020 - Implement a Dictionary in Swift It is not just a HashMap…but how can we implement our own version of a Dictionary in Swift? Dictionaries are a data type that are a function of hash table …
🌐
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 - 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 really powerful data structures when well implemented. Basically their insertion and fetch are made in constant time — O(1) if well implemented.
🌐
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 ...
import Foundation let strings = Set([ "one", "two", "three" ]) let ints = strings.map { $0.count } print(ints) Run the contents of the playground to see the result. You can verify that the order of the elements is undefined if you run the contents of the playground several times. It should be no surprise that Swift's map(_:) method can also be used to transform the keys and values of a dictionary.