With so many terms for syntax and functionality in Swift it's easy to feel confused sometimes. So, this page acts as a one-stop dictionary of terms, providing definitions for all common terms used in the Swift language.
assert()
function.switch
blocks that allows code to handle enum cases that may be added at some point in the future, without breaking source compatibility.open
means the property can be accessed and overridden from anywhere, public
means the property may be accessed from anywhere but overridden only within the module it came from, internal
means the property may be accessed from anywhere inside the same module, fileprivate
means the property may be accessed from anywhere inside the same file, and private
means the property may be accessed from anywhere inside the same type.sayHello(to: "Paul")
the to
part is an argument. Many people just say "parameter" rather than "argument", but argument is technically correct.associatedtype
.break myLoop
, it will break out of the specified block.allCases
array that lets you loop over the cases in the enum.do
.Double
or Float
depending on the platform.<
.#
, that act as instructions to the compiler. For example, compiler directives can check whether we're targeting the simulator or not, and compile one of two code variants.score += 1
adds 1 to the current value of score
.if
statement. You can provide code to run when your condition is true, as well as an else
Equatable
only if their element also conforms to Equatable
.continue myLoop
, it will continue the specified block.switch
blocks that will match all other values.func checkSettings(debugMode: Bool = true)
can be called as checkSettings(debugMode: true)
or checkSettings(debugMode: false)
, but also as checkSettings()
– missing a debugMode
value will assume true, because that's the default value.catch
.==
.12 * 12
evaluates to 144.Int
.switch
blocks to mean "carry on executing the case immediately following this one."init?()
or init!()
.guard let
, which checks whether an optional has a value, and, if it does, creates a new constant for that optional's value so it can be used safely. If it has no value, the guard
condition fails and you must exit the current scope.if
condition fails and you can run an else
block instead.class
, continue
, and try
.??
, that uses the value from an optional if it has one, or a default value otherwise.throws
, and must not use the throw
keyword.2 + 3
the 2 and 3 are operands.+
is an operator that adds two values together.+
to do multiple things depending on how it's used. For example, 1 + 1
is an integer addition, but "Hello " + "Paul"
will join the strings together.String
or Int
, but adds the ability to store no value at all. "No value" is different from all regular integer values, including zero. Swift uses optionals heavily as a way of providing runtime safety, and the compiler forces us to use them correctly.user?.name?.uppercased()
.override
keyword tells the Swift compiler you understand that you are changing the behavior.func sayHello(to: String)
, the to
part is a parameter.sayHello(to name: String)
, people calling the function will say sayHello(to: "Paul")
, but inside the function you would refer to name
.Labrador
class instance could also be used as a Dog
and Mammal
if you had defined those as parent classes.willSet
and didSet
, that gets called whenever a property is being changed.@UserDefaults
property wrapper to make loading and saving data to user defaults easier.Equatable
.Comparable
protocol inherits from Equatable
.1..<4
includes the numbers 1, 2, and 3, whereas the range 1...4
includes the numbers 1, 2, 3, and 4. Ranges can also be made from other data types, such as dates.rethrows
keyword so that it throws errors only if the closure it accepts throws errors.Hashable
protocol.name
string and an age
integer, you could refer to the string as $0
and the age as $1
. Note: you may not mix and match shorthand syntax with regular syntax.some View
in SwiftUI to mean "some sort of View will be returned but it doesn't matter which type specifically."print(age)
.static
.String
that must be hand-typed – you must literally type the string directly into your code rather than using string interpolation.someArray[3]
is a subscript, as is someDictionary["name"]
.switch age
to evaluate the age
variable, then have cases for 0 to 10, 10 to 20, 20 to 30, and so on. Switch blocks must be exhaustive in Swift, which means they must have cases to cover all possible values.[String]
is syntactic sugar for Array<String>
.Equatable
, and all its properties already conform to Equatable
, then Swift can synthesize a ==
function for it.? :
. For example, isEnabled ? 10 : 100
will return 10 if isEnabled
is true, and 100 if it's false.throws
keyword in Swift, and called using try
.do
block, then call any throwing methods inside that using try
, and finally add one or more catch
blocks to catch any errors. Writing a catch
block to catch all errors is sometimes called a Pokémon catch, because "you gotta catch 'em all."typealias
.print()
function is variadic, because you can write print(1)
to print a single value, or print(1, 2, 3, 4, 5, 6, 7, 8)
to print many.Link copied to your pasteboard.