- May 10, 2024
- Posted by: Harshil Vyas
- Category: Mobile Development
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.Β Β