Extensions

Extensions Add functionality to an existing type. Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you don’t have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions don’t have names.) Extensions in Swift can: Add computed instance properties and computed type properties Define instance methods and type methods Provide new initializers Define subscripts Define and use new nested types Make an existing type conform to a protocol In Swift, you can even extend a protocol to provide implementations of its requirements or add additional functionality that conforming types can take advantage of....

December 1, 2023

Functions

Functions Define and call functions, label their arguments, and use their return values. Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with names and argument labels for each parameter....

December 1, 2023

Generics

Generics Write code that works for multiple types and specify requirements for those types. Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner. Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code....

December 1, 2023

Inheritance

Inheritance Subclass to add or override functionality. A class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Inheritance is a fundamental behavior that differentiates classes from other types in Swift. Classes in Swift can call and access methods, properties, and subscripts belonging to their superclass and can provide their own overriding versions of those methods, properties, and subscripts to refine or modify their behavior....

December 1, 2023

Initialization

Initialization Set the initial values for a type’s stored properties and perform one-time setup. Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that’s required before the new instance is ready for use. You implement this initialization process by defining initializers, which are like special methods that can be called to create a new instance of a particular type....

December 1, 2023

Macros

title: “Macros” date: 2023-05-01 description: “Learn how to use macros to generate code at compile time in Swift” weight: 180 type: “book” layout: “chapter” Use macros to generate code at compile time. Macros transform your source code when you compile it, letting you avoid writing repetitive code by hand. During compilation, Swift expands any macros in your code before building your code as usual. Expanding a macro is always an additive operation: Macros add new code, but they never delete or modify existing code....

December 1, 2023

Memory Safety

Memory Safety Structure your code to avoid conflicts when accessing memory. By default, Swift prevents unsafe behavior from happening in your code. For example, Swift ensures that variables are initialized before they’re used, memory isn’t accessed after it’s been deallocated, and array indices are checked for out-of-bounds errors. Swift also makes sure that multiple accesses to the same area of memory don’t conflict, by requiring code that modifies a location in memory to have exclusive access to that memory....

December 1, 2023

Methods

Methods Define and call functions that are part of an instance or type. Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C....

December 1, 2023

Nested Types

Nested Types Define types inside the scope of another type. Enumerations are often created to support a specific class or structure’s functionality. Similarly, it can be convenient to define utility structures purely for use within the context of a more complex type, and protocols that are normally used in conjunction with a specific type. To accomplish this, Swift enables you to define nested types, whereby you nest supporting types like enumerations, structures, and protocols within the definition of the type they support....

December 1, 2023

Opaque Types

Opaque and Boxed Protocol Types Hide implementation details about a value’s type. Swift provides two ways to hide details about a value’s type: opaque types and boxed protocol types. Hiding type information is useful at boundaries between a module and code that calls into the module, because the underlying type of the return value can remain private. A function or method that returns an opaque type hides its return value’s type information....

December 1, 2023