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