Complete Guide of Data Structures in Swift

Data modelling is the foundation of computer science and software development. They help us store and protect good information. Swift programming language is a powerful and intuitive language developed by Apple that provides a variety of design documents to choose from. and provide examples to help you understand how to use and apply them.

Data Structures available in Swift

  • Arrays  
  • Dictionaries  
  • Sets  
  • Stacks  
  • Queues  
  • Linked Lists  
  • Trees (Binary Trees)  
  • Graphs  

1. Arrays

 Arrays in Swift are ordered collections of elements with dimension. You can save these elements of the same type in an array. Here’s how to create and use arrays: 

// 𝙲𝚛𝚎𝚊𝚝𝚎 𝚊𝚗 𝚊𝚛𝚛𝚊𝚢 𝚘𝚏 𝚒𝚗𝚝𝚎𝚐𝚎𝚛𝚜
𝚟𝚊𝚛 𝚊𝚛𝚛𝙽𝚞𝚖𝚋𝚎𝚛𝚜 = [𝟷, 𝟸, 𝟹, 𝟺, 𝟻]

// 𝙰𝚌𝚌𝚎𝚜𝚜 𝚎𝚕𝚎𝚖𝚎𝚗𝚝𝚜
𝚕𝚎𝚝 𝚏𝚒𝚛𝚜𝚝𝙴𝚕𝚎𝚖𝚎𝚗𝚝 = 𝚊𝚛𝚛𝙽𝚞𝚖𝚋𝚎𝚛𝚜[0]

/ /𝙲𝚑𝚊𝚗𝚐𝚎 𝚝𝚑𝚎 𝚌𝚘𝚗𝚝𝚎𝚗𝚝
𝚊𝚛𝚛𝙽𝚞𝚖𝚋𝚎𝚛𝚜.𝚊𝚙𝚙𝚎𝚗𝚍(𝟽)
𝚊𝚛𝚛𝙽𝚞𝚖𝚋𝚎𝚛𝚜[𝟸] = 𝟷𝟸

//𝚒𝚝𝚎𝚛𝚊𝚝𝚒𝚘𝚗 𝚘𝚏 𝚊𝚛𝚛𝚊𝚢
𝚏𝚘𝚛 𝚗𝚞𝚖𝚋𝚎𝚛 𝚒𝚗 𝚊𝚛𝚛𝙽𝚞𝚖𝚋𝚎𝚛𝚜 {
𝚙𝚛𝚒𝚗𝚝(𝚗𝚞𝚖𝚋𝚎𝚛)

}[𝚃𝚎𝚡𝚝 𝚆𝚛𝚊𝚙𝚙𝚒𝚗𝚐 𝙱𝚛𝚎𝚊𝚔]𝚄𝚜𝚊𝚐𝚎 𝚜𝚌𝚎𝚗𝚊𝚛𝚒𝚘𝚜:

Arrays are designed for situations where a list of items needs to be stored and accessed in a specific order. They are useful for accessing parameters, but may not be the best choice for adding or deleting.[𝚃𝚎𝚡𝚝 𝚆𝚛𝚊𝚙𝚙𝚒𝚗𝚐 𝙱𝚛𝚎𝚊𝚔]  

2. Dictionaries: 

A dictionary in Swift is a collection of key pairs. Each key in the dictionary is unique and you can use this key to store the corresponding value.

// 𝙲𝚛𝚎𝚊𝚝𝚎 𝚍𝚒𝚌𝚝𝚒𝚘𝚗𝚊𝚛𝚢

𝚟𝚊𝚛 𝚂𝚝𝚞𝚍𝚎𝚗𝚝𝙶𝚛𝚊𝚍𝚎𝚜 = ["𝙴𝚖𝚒𝚕𝚒": 𝟿0, "𝙱𝚘𝚋𝚋𝚢": 𝟾𝟻, “𝙲𝚑𝚊𝚛𝚕𝚒𝚎 𝙲𝚑𝚊𝚙𝚕𝚒𝚗 ": 𝟿𝟸]

// 𝙰𝚌𝚌𝚎𝚜𝚜 𝚟𝚊𝚕𝚞𝚎𝚜
𝚕𝚎𝚝 𝚘𝚋𝚓𝙴𝚖𝚒𝚕𝚒= 𝚂𝚝𝚞𝚍𝚎𝚗𝚝𝙶𝚛𝚊𝚍𝚎𝚜[ "𝙴𝚖𝚒𝚕𝚒" ]

// 𝙲𝚑𝚊𝚗𝚐𝚎 𝚝𝚑𝚎 𝚟𝚊𝚕𝚞𝚎
𝚜𝚝𝚞𝚍𝚎𝚗𝚝𝙶𝚛𝚊𝚍𝚎𝚜["𝙳𝚑𝚊𝚠𝚊𝚗"] = 𝟾𝟾

// 𝙱𝚛𝚘𝚠𝚜𝚎 𝚝𝚑𝚎 𝚍𝚒𝚌𝚝𝚒𝚘𝚗𝚊𝚛𝚢

𝚏𝚘𝚛 (𝚗𝚊𝚖𝚎, 𝚐𝚛𝚊𝚍𝚎) 𝚒𝚗 𝚂𝚝𝚞𝚍𝚎𝚗𝚝𝚜𝙶𝚛𝚊𝚍𝚎𝚜 { [𝚃𝚎𝚡𝚝 𝚆𝚛𝚊𝚙𝚙𝚒𝚗𝚐 𝙱𝚛𝚎𝚊𝚔] 𝚙𝚛𝚒𝚗𝚝(" \( 𝙽𝚊𝚖𝚎) :\(𝚍𝚎𝚐𝚛𝚎𝚎)")

}

Usage scenarios: Dictionaries are ideal when you need to associate results with specific keys. They are useful for quick searches.  

3. Sets:  

Collections in Swift are unordered collections of unique values. They make everything happen at once.

// 𝙲𝚛𝚎𝚊𝚝𝚒𝚗𝚐 𝚊 𝚜𝚎𝚝 𝚌𝚘𝚕𝚕𝚎𝚌𝚝𝚒𝚘𝚗𝚜 𝚘𝚏 𝚒𝚗𝚝𝚎𝚐𝚎𝚛𝚜
𝚟𝚊𝚛 𝚘𝚋𝚓𝚄𝚗𝚚𝙽𝚞𝚖𝚋𝚎𝚛𝚜: 𝚂𝚎𝚝<𝙸𝚗𝚝> = [𝟷, 𝟸, 𝟹, 𝟹, 𝟺, 𝟻][𝚃𝚎𝚡𝚝 𝚆𝚛𝚊𝚙𝚙𝚒𝚗𝚐 𝙱𝚛𝚎𝚊𝚔]

// 𝙰𝚍𝚍 𝚊𝚗𝚍 𝚛𝚎𝚖𝚘𝚟𝚎 𝚎𝚕𝚎𝚖𝚎𝚗𝚝𝚜
𝚘𝚋𝚓𝚄𝚗𝚚𝙽𝚞𝚖𝚋𝚎𝚛𝚜.𝚒𝚗𝚜𝚎𝚛𝚝(𝟼)
𝚘𝚋𝚓𝚄𝚗𝚚𝙽𝚞𝚖𝚋𝚎𝚛𝚜.𝚛𝚎𝚖𝚘𝚟𝚎(𝟹)

// 𝙲𝚑𝚎𝚌𝚔𝚒𝚗𝚐 𝚖𝚎𝚖𝚋𝚎𝚛𝚜𝚑𝚒𝚙
𝚕𝚎𝚝 𝚌𝚘𝚗𝚝𝚊𝚒𝚗𝚜𝙵𝚒𝚟𝚎 = 𝚘𝚋𝚓𝚄𝚗𝚚𝙽𝚞𝚖𝚋𝚎𝚛𝚜.𝚌𝚘𝚗𝚝𝚊𝚒𝚗𝚜(𝟻)

Usage scenarios: Sets are useful when you need to ensure the uniqueness of elements or when you want to perform set operations like union, intersection, and difference.  

4. Stacks:  

A stack is a data structure that executes the Last-In-First-Out (LIFO) theory. You can push elements onto the stack and pop them off the stack in return order.

 

// 𝙲𝚛𝚎𝚊𝚝𝚒𝚗𝚐 𝚊 𝚜𝚝𝚊𝚌𝚔 𝚘𝚏 𝚒𝚝𝚎𝚖𝚜
𝚟𝚊𝚛 𝚘𝚋𝚓𝚂𝚝𝚊𝚌𝚔: [𝙸𝚗𝚝] = []

// 𝙿𝚞𝚜𝚑𝚒𝚗𝚐 𝚒𝚝𝚎𝚖𝚜 𝚘𝚗𝚝𝚘 𝚝𝚑𝚎 𝚜𝚝𝚊𝚌𝚔
𝚘𝚋𝚓𝚂𝚝𝚊𝚌𝚔.𝚊𝚙𝚙𝚎𝚗𝚍(𝟷)
𝚘𝚋𝚓𝚂𝚝𝚊𝚌𝚔.𝚊𝚙𝚙𝚎𝚗𝚍(𝟸)
𝚘𝚋𝚓𝚂𝚝𝚊𝚌𝚔.𝚊𝚙𝚙𝚎𝚗𝚍(𝟹)

// 𝙿𝚘𝚙𝚙𝚒𝚗𝚐 𝚒𝚝𝚎𝚖𝚜
𝚕𝚎𝚝 𝚙𝚘𝚙𝙸𝚝𝚎𝚖 = 𝚘𝚋𝚓𝚂𝚝𝚊𝚌𝚔.𝚙𝚘𝚙𝙻𝚊𝚜𝚝()

Usage scenarios: Sets are useful when you need to ensure the uniqueness of elements or when you want to perform set operations like union, intersection, and difference.  

5. Queues :

Queues are data resources that follow the First-In-First-Out (FIFO) principle. Elements are included to the rear and removed from the front of the queue.

 

// 𝙲𝚛𝚎𝚊𝚝𝚒𝚗𝚐 𝚊 𝚚𝚞𝚎𝚞𝚎 𝚒𝚝𝚎𝚖𝚜
𝚟𝚊𝚛 𝚘𝚋𝚓𝚀𝚞𝚎𝚞𝚎: [𝙸𝚗𝚝] = []

// 𝙴𝚗𝚚𝚞𝚎𝚞𝚒𝚗𝚐 𝚒𝚝𝚎𝚖𝚜
𝚘𝚋𝚓𝚀𝚞𝚎𝚞𝚎.𝚊𝚙𝚙𝚎𝚗𝚍(𝟷)
𝚘𝚋𝚓𝚀𝚞𝚎𝚞𝚎.𝚊𝚙𝚙𝚎𝚗𝚍(𝟸)
𝚘𝚋𝚓𝚀𝚞𝚎𝚞𝚎.𝚊𝚙𝚙𝚎𝚗𝚍(𝟹)

// 𝙳𝚎𝚚𝚞𝚎𝚞𝚒𝚗𝚐 𝚒𝚝𝚎𝚖𝚜
𝚕𝚎𝚝 𝚍𝚎𝚚𝚞𝚎𝚞𝚎𝚍𝙸𝚝𝚎𝚖 = 𝚘𝚋𝚓𝚀𝚞𝚎𝚞𝚎.𝚛𝚎𝚖𝚘𝚟𝚎𝙵𝚒𝚛𝚜𝚝()

Usage scenarios: Queues are essential for scenarios like task scheduling, managing print jobs, and implementing algorithms like Breadth-First Search.  

6. Linked Lists:  

Linked lists consist of nodes where each node contains a value and a reference (or link) to the next node. Linked lists come in various forms, including singly linked lists, doubly linked lists, and circular linked lists.  

// Node model of singly linked list  
class Nod {  
    var value: T  
    var next: Nod?  
    init(_ value: T) {  
        self.value = value  
    }  
}  
  
// Create a one-way link list  
let objNod1 = Nod(1)  
let objNod2 = Nod(2)  
let objNod3 = Nod(3)   
objNod1.next = objNod2  
objNod2.next = objNod3

Usage scenarios: Linked lists are suitable when you need dynamic resizing or when you frequently insert or delete elements in the middle of the list.  

7. Trees (Binary Trees):    

Trees are hierarchical data configurations with a root node and child nodes. Binary trees have at most two child nodes per parent node.

// 𝚂𝚝𝚛𝚞𝚌𝚝𝚞𝚛𝚎 𝚘𝚏 𝚝𝚑𝚎 𝚋𝚒𝚗𝚊𝚛𝚢 𝚝𝚛𝚎𝚎
𝚌𝚕𝚊𝚜𝚜 𝚃𝚛𝚎𝚎𝙽𝚘𝚍𝚎<𝚃> {
𝚟𝚊𝚛 𝚟𝚊𝚕𝚞𝚎: 𝚃
𝚟𝚊𝚛 𝚕𝚎𝚏𝚝: 𝚃𝚛𝚎𝚎𝙽𝚘𝚍𝚎?
𝚟𝚊𝚛 𝚛𝚒𝚐𝚑𝚝: 𝚃𝚛𝚎𝚎𝙽𝚘𝚍𝚎?
𝚒𝚗𝚒𝚝(_ 𝚟𝚊𝚕𝚞𝚎: 𝚃) {
𝚜𝚎𝚕𝚏.𝚟𝚊𝚕𝚞𝚎 = 𝚟𝚊𝚕𝚞𝚎
}
}

// 𝙲𝚛𝚎𝚊𝚝𝚎 𝚊 𝚝𝚛𝚎𝚎
𝚕𝚎𝚝 𝚘𝚋𝚓𝚁𝚘𝚘𝚝 = 𝚃𝚛𝚎𝚎𝙽𝚘𝚍𝚎(𝟷)
𝚘𝚋𝚓𝚁𝚘𝚘𝚝.𝚕𝚎𝚏𝚝 = 𝚃𝚛𝚎𝚎𝙽𝚘𝚍𝚎(𝟸)
𝚘𝚋𝚓𝚁𝚘𝚘𝚝.𝚛𝚒𝚐𝚑𝚝 = 𝚃𝚛𝚎𝚎𝙽𝚘𝚍𝚎(𝟹)

Usage scenarios: Binary trees are designed for many applications, including hierarchical data representation and search and sorting algorithms (such as binary search trees).

8. Graphs:  

A graph is a complex file consisting of nodes (vertices) and edges connecting the nodes. They can be direct or indirect and have a variety of representations, including adjacency lists and adjacency matrices.

// 𝙲𝚛𝚎𝚊𝚝𝚒𝚗𝚐 𝚊𝚗 𝚖𝚢𝙰𝚍𝚓𝚊𝚌𝚎𝚗𝚌𝚢 𝚕𝚒𝚜𝚝 𝚏𝚘𝚛 𝚊 𝚐𝚛𝚊𝚙𝚑
𝚌𝚕𝚊𝚜𝚜 𝙶𝚛𝚊𝚙𝚑 {
𝚟𝚊𝚛 𝚖𝚢𝙰𝚍𝚓𝚊𝚌𝚎𝚗𝚌𝚢𝙻𝚒𝚜𝚝: [𝙸𝚗𝚝: [𝙸𝚗𝚝]] = [:]
𝚏𝚞𝚗𝚌 𝚊𝚍𝚍𝚒𝚗𝚐𝙴𝚍𝚐𝚎(𝚏𝚛𝚘𝚖 𝚘𝚋𝚓𝚂𝚘𝚞𝚛𝚌𝚎: 𝙸𝚗𝚝, 𝚝𝚘 𝚘𝚋𝚓𝙳𝚎𝚜𝚝𝚒𝚗𝚊𝚝𝚒𝚘𝚗: 𝙸𝚗𝚝) {
𝚒𝚏 𝚖𝚢𝙰𝚍𝚓𝚊𝚌𝚎𝚗𝚌𝚢𝙻𝚒𝚜𝚝[𝚘𝚋𝚓𝚂𝚘𝚞𝚛𝚌𝚎] == 𝚗𝚒𝚕 {
𝚖𝚢𝙰𝚍𝚓𝚊𝚌𝚎𝚗𝚌𝚢𝙻𝚒𝚜𝚝[𝚘𝚋𝚓𝚂𝚘𝚞𝚛𝚌𝚎] = []
}
𝚖𝚢𝙰𝚍𝚓𝚊𝚌𝚎𝚗𝚌𝚢𝙻𝚒𝚜𝚝[𝚘𝚋𝚓𝚂𝚘𝚞𝚛𝚌𝚎]?.𝚊𝚙𝚙𝚎𝚗𝚍(𝚘𝚋𝚓𝙳𝚎𝚜𝚝𝚒𝚗𝚊𝚝𝚒𝚘𝚗)
}
}

// 𝙲𝚛𝚎𝚊𝚝𝚎 𝚊 𝚐𝚛𝚊𝚙𝚑 𝚜𝚝𝚛𝚞𝚌𝚝𝚞𝚛𝚎
𝚕𝚎𝚝 𝚘𝚋𝚓𝙶𝚛𝚊𝚙𝚑 = 𝙶𝚛𝚊𝚙𝚑()
𝚘𝚋𝚓𝙶𝚛𝚊𝚙𝚑.𝚊𝚍𝚍𝚒𝚗𝚐𝙴𝚍𝚐𝚎(𝚏𝚛𝚘𝚖: 𝟷, 𝚝𝚘: 𝟸)
𝚘𝚋𝚓𝙶𝚛𝚊𝚙𝚑.𝚊𝚍𝚍𝚒𝚗𝚐𝙴𝚍𝚐𝚎(𝚏𝚛𝚘𝚖: 𝟷, 𝚝𝚘: 𝟹)
𝚘𝚋𝚓𝙶𝚛𝚊𝚙𝚑.𝚊𝚍𝚍𝚒𝚗𝚐𝙴𝚍𝚐𝚎(𝚏𝚛𝚘𝚖: 𝟸, 𝚝𝚘: 𝟺)

Usage scenarios: Graphs structures are used to model relationships and solve many problems such as pathfinding, network analysis, and recommendations.  

Conclusion

In this blog, we learn the basics of using data structures in Swift. Data models play an important role in computer science and programming. Swift’s advantage is to provide a set of data structures that you can use to create useful and efficient programs. Understanding these files and their usage information is crucial to becoming a skilled Swift developer. As you gain experience, you can choose the right materials for each problem you encounter and ultimately become more efficient and effective.   

how can we help you?

Contact us at the Consulting WP office nearest to you or submit a business inquiry online.

Get technology solution for your business need