Catalog / Swift Cheatsheet
Swift Cheatsheet
A comprehensive guide to Swift programming language syntax, features, and best practices, designed for quick reference and efficient learning.
Swift Basics
Variables and Constants
|
Declares a variable (mutable).
|
|
Declares a constant (immutable).
|
Type Annotation |
Explicitly specify the type of a variable or constant.
|
Type Inference |
Swift automatically infers the type.
|
Optional Variables |
Variables that may or may not have a value. Defined with
|
Optional Binding |
Safely unwrap optionals using
|
Data Types
Basic Data Types:
|
Collection Types:
|
Tuples:
|
Operators
Arithmetic Operators |
|
Comparison Operators |
|
Logical Operators |
|
Assignment Operators |
|
Range Operators |
|
Control Flow
Conditional Statements
|
|
Loops
|
Iterates over a sequence (e.g., array, range).
|
|
Executes a block of code as long as a condition is true.
|
|
Similar to
|
Control Transfer Statements
Terminates the execution of a loop or switch statement. |
Skips the rest of the current iteration and starts the next iteration. |
In |
Exits from a function or method. |
Functions and Closures
Functions
Function Definition
|
Function Parameters
|
Function with Multiple Return Values (Tuples)
|
Closures
Closure Expression Syntax
|
Example: Using a closure to sort an array
|
Trailing Closures If a closure is the last argument to a function, it can be written after the function call’s parentheses.
|
Shorthand Argument Names Swift automatically provides shorthand argument names
|
Structures and Classes
Structures
Structure Definition
|
Structures are Value Types When a structure is assigned to a new variable, a copy of the structure is created.
|
Classes
Class Definition
|
Classes are Reference Types When a class instance is assigned to a new variable, a reference to the original instance is created.
|
Inheritance Classes can inherit properties and methods from other classes.
|
Protocols
Protocol Definition
|