Classes are reference types used to define reusable and flexible blueprints for objects. Classes can have properties, methods, initialisers, deinitialisers and can conform to protocols. They also support inheritance, which makes them powerful for building complex systems. Characteristic of Class Initialiser(Constructor) Classes can have multiple initialisers, and these can be …
Closures
Closures are self-contained blocks of functionality that can be passed around and used in your code. They can capture and store references to variables and constants from their surrounding context, making them extremely powerful for functional programming. 1.Closure as a Function Argument 2.Trailing Closure If a closure is the last …
Optionals in Swift
Optionals in Swift are a powerful feature that allows a variable to hold either a value or nil (no value). This helps handle the absence of a value safely and avoids runtime crashes caused by null pointers. Unwrapping Optionals 1.Force Unwrapping Access the value of an optional using ! if …
Enums
Enums define a group of related values in a type-safe way. They are particularly useful for defining states, options, or categories that a variable can have. Raw Values Enums can have raw values associated with each case. The raw value must be of the same type and unique. Associated Values …
Sets
A Set in Swift is an unordered collection of unique elements. Unlike an Array, it does not allow duplicate values and does not maintain the order of elements. Key Set Operations Set Operations 1. Union Combines all unique elements from two sets. 2.Intersection Finds common elements between two sets. Difference …
Dictionary
A dictionary in Swift is a collection type that stores key-value pairs. Each key is unique and you use it to access its corresponding value. Iterating Over a Dictionary
Array
Array is an ordered, random-access collection type. Arrays are one of the most commonly used data types in an app. We use the Array type to hold elements of a single type, the array’s Element type. An array can store any kind. Empty arrays The following three declarations are equivalent: …
Operators in Swift
Types of Operators 1. Assignment Operator Assigns the value on the right-hand side to the variable or constant on the left-hand side. (=) 2. Arithmetic Operators Swift provides standard arithmetic operators for mathematical operations: 3. Compound Assignment Operators These combine an operation with assignment, allowing you to update a variable …