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.
switchblocks that allows code to handle enum cases that may be added at some point in the future, without breaking source compatibility.
openmeans the property can be accessed and overridden from anywhere,
publicmeans the property may be accessed from anywhere but overridden only within the module it came from,
internalmeans the property may be accessed from anywhere inside the same module,
fileprivatemeans the property may be accessed from anywhere inside the same file, and
privatemeans the property may be accessed from anywhere inside the same type.
topart is an argument. Many people just say "parameter" rather than "argument", but argument is technically correct.
break myLoop, it will break out of the specified block.
allCasesarray that lets you loop over the cases in the enum.
Floatdepending 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 += 1adds 1 to the current value of
ifstatement. You can provide code to run when your condition is true, as well as an
Equatableonly if their element also conforms to
continue myLoop, it will continue the specified block.
switchblocks that will match all other values.
func checkSettings(debugMode: Bool = true)can be called as
checkSettings(debugMode: false), but also as
checkSettings()– missing a
debugModevalue will assume true, because that's the default value.
12 * 12evaluates to 144.
switchblocks to mean "carry on executing the case immediately following this one."
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
guardcondition fails and you must exit the current scope.
ifcondition fails and you can run an
??, that uses the value from an optional if it has one, or a default value otherwise.
throws, and must not use the
2 + 3the 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 + 1is an integer addition, but
"Hello " + "Paul"will join the strings together.
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.
overridekeyword tells the Swift compiler you understand that you are changing the behavior.
func sayHello(to: String), the
topart is a parameter.
sayHello(to name: String), people calling the function will say
sayHello(to: "Paul"), but inside the function you would refer to
Labradorclass instance could also be used as a
Mammalif you had defined those as parent classes.
didSet, that gets called whenever a property is being changed.
@UserDefaultsproperty wrapper to make loading and saving data to user defaults easier.
Comparableprotocol inherits from
1..<4includes the numbers 1, 2, and 3, whereas the range
1...4includes the numbers 1, 2, 3, and 4. Ranges can also be made from other data types, such as dates.
rethrowskeyword so that it throws errors only if the closure it accepts throws errors.
namestring and an
ageinteger, you could refer to the string as
$0and the age as
$1. Note: you may not mix and match shorthand syntax with regular syntax.
some Viewin SwiftUI to mean "some sort of View will be returned but it doesn't matter which type specifically."
Stringthat must be hand-typed – you must literally type the string directly into your code rather than using string interpolation.
someArrayis a subscript, as is
switch ageto evaluate the
agevariable, 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
Equatable, and all its properties already conform to
Equatable, then Swift can synthesize a
==function for it.
? :. For example,
isEnabled ? 10 : 100will return 10 if
isEnabledis true, and 100 if it's false.
throwskeyword in Swift, and called using
doblock, then call any throwing methods inside that using
try, and finally add one or more
catchblocks to catch any errors. Writing a
catchblock to catch all errors is sometimes called a Pokémon catch, because "you gotta catch 'em all."
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.