Automatic Reference Counting

Automatic Reference Counting Model the lifetime of objects and their relationships. Swift uses Automatic Reference Counting (ARC) to track and manage your app’s memory usage. In most cases, this means that memory management “just works” in Swift, and you don’t need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed. However, in a few cases ARC requires more information about the relationships between parts of your code in order to manage memory for you....

December 1, 2023

Basic Operators

Basic Operators Perform operations like assignment, arithmetic, and comparison. An operator is a special symbol or phrase that you use to check, change, or combine values. For example, the addition operator (+) adds two numbers, as in let i = 1 + 2, and the logical AND operator (&&) combines two Boolean values, as in if enteredDoorCode && passedRetinaScan. Swift supports the operators you may already know from languages like C, and improves several capabilities to eliminate common coding errors....

December 1, 2023

Classes and Structures

Structures and Classes Model custom types that encapsulate data. Structures and classes are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your structures and classes using the same syntax you use to define constants, variables, and functions. Unlike other programming languages, Swift doesn’t require you to create separate interface and implementation files for custom structures and classes. In Swift, you define a structure or class in a single file, and the external interface to that class or structure is automatically made available for other code to use....

December 1, 2023

Closures

Closures Group code that executes together, without creating a named function. Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to closures, anonymous functions, lambdas, and blocks in other programming languages. Closures can capture and store references to any constants and variables from the context in which they’re defined. This is known as closing over those constants and variables....

December 1, 2023

Collection Types

Collection Types Organize data using arrays, sets, and dictionaries. Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you can’t insert a value of the wrong type into a collection by mistake....

December 1, 2023

Concurrency

Concurrency Perform asynchronous operations. Swift has built-in support for writing asynchronous and parallel code in a structured way. Asynchronous code can be suspended and resumed later, although only one piece of the program executes at a time. Suspending and resuming code in your program lets it continue to make progress on short-term operations like updating its UI while continuing to work on long-running operations like fetching data over the network or parsing files....

December 1, 2023

Control Flow

Control Flow Structure code with branches, loops, and early exits. Swift provides a variety of control flow statements. These include while loops to perform a task multiple times; if, guard, and switch statements to execute different branches of code based on certain conditions; and statements such as break and continue to transfer the flow of execution to another point in your code. Swift provides a for-in loop that makes it easy to iterate over arrays, dictionaries, ranges, strings, and other sequences....

December 1, 2023

Deinitialization

Deinitialization Release resources that require custom cleanup. A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how initializers are written with the init keyword. Deinitializers are only available on class types. How Deinitialization Works Swift automatically deallocates your instances when they’re no longer needed, to free up resources. Swift handles the memory management of instances through automatic reference counting (ARC), as described in doc:AutomaticReferenceCounting....

December 1, 2023

Enumerations

Enumerations Model custom types that define a list of possible values. An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code. If you are familiar with C, you will know that C enumerations assign related names to a set of integer values. Enumerations in Swift are much more flexible, and don’t have to provide a value for each case of the enumeration....

December 1, 2023

Error Handling

Error Handling Respond to and recover from errors. Error handling is the process of responding to and recovering from error conditions in your program. Swift provides first-class support for throwing, catching, propagating, and manipulating recoverable errors at runtime. Some operations aren’t guaranteed to always complete execution or produce a useful output. Optionals are used to represent the absence of a value, but when an operation fails, it’s often useful to understand what caused the failure, so that your code can respond accordingly....

December 1, 2023