Swift Cheat Sheet

In iOS, Swift

Swift is a powerful and intuitive programming language developed by Apple for building iOS, macOS, watchOS, and tvOS applications. It was first introduced in 2014 and has since become one of the most popular programming languages in the world.

Swift is designed to be easy to learn and use, with a syntax that is concise and expressive. It is also highly efficient, with performance that is comparable to that of C++ and Objective-C. This makes it an ideal choice for developing high-performance applications that run smoothly on Apple devices.

This cheat sheet provides an overview of the most important concepts and syntax in Swift. It is divided into different sections for easy navigation.

Basics

SyntaxDescription
var variableName: Type = valueDeclares a variable with an initial value
let constantName: Type = valueDeclares a constant with an initial value
func functionName(parameterName: Type) -> ReturnTypeDeclares a function with a parameter and return type
if condition { ... } else { ... }Executes code based on a condition
for item in collection { ... }Loops over a collection of items
while condition { ... }Loops while a condition is true
switch value { case pattern: ... }Executes code based on a value matching a pattern
guard condition else { ... }Exits a function if a condition is not met
try expressionExecutes an expression that can throw an error
do { ... } catch { ... }Handles errors thrown by an expression

Optionals

SyntaxDescription
var optionalValue: Type?Declares an optional variable
optionalValue = nilSets an optional variable to nil
if let unwrappedValue = optionalValue { ... }Unwraps an optional value if it is not nil
guard let unwrappedValue = optionalValue else { ... }Exits a function if an optional value is nil
optionalValue ?? defaultValueReturns a default value if an optional value is nil

Collections

SyntaxDescription
var array: [Type] = [value1, value2, ...]Declares an array
array.append(value)Adds a value to an array
array.remove(at: index)Removes a value from an array
var dictionary: [KeyType: ValueType] = [key1: value1, key2: value2, ...]Declares a dictionary
dictionary[key] = valueAdds a key-value pair to a dictionary
dictionary.removeValue(forKey: key)Removes a key-value pair from a dictionary

Classes and Structs

SyntaxDescription
class ClassName { ... }Declares a class
struct StructName { ... }Declares a struct
var instance = ClassName()Creates an instance of a class
var instance = StructName()Creates an instance of a struct
class ClassName: SuperclassName { ... }Declares a subclass
struct StructName: Protocol1, Protocol2 { ... }Declares a struct that conforms to multiple protocols
init(parameterName: Type) { ... }Declares an initializer
deinit { ... }Declares a deinitializer
var property: Type = valueDeclares a property
func methodName(parameterName: Type) -> ReturnType { ... }Declares a method

Closures

SyntaxDescription
{ (parameters) -> ReturnType in ... }Declares a closure
array.map { (item) -> ReturnType in ... }Applies a closure to each item in an array
array.filter { (item) -> Bool in ... }Filters an array based on a closure
array.reduce(initialValue) { (result, item) -> ReturnType in ... }Reduces an array to a single value based on a closure

References