Software developers have long understood the many benefits of code reuse. These benefits include an increase in productivity and code quality, and a decrease in testing and maintenance costs. The idea of reducing redundancy in a codebase was popularized by the DRY principle, which was introduced in the 1999 book titled “The Pragmatic Programmer: From Journeyman to Master“. The acronym DRY stands for Don’t Repeat Yourself, and the principle suggests that “Every piece of knowledge must have a single, unambiguous,… [continue]
Swift Essentials: Protocols
A protocol is an interface that defines a set of properties and methods which are necessary for a particular piece of functionality. The protocol can then be adopted by a class or structure to provide the actual implementation of that functionality. In this way, a protocol is a form of encapsulation, which allows you to interact with the interface without concern for the particular implementation type, which may be injected at runtime. This makes protocols ideal for delegation, which is a design pattern wherein… [continue]
Swift Essentials: Extensions
Retroactive modeling is the practice of using existing types to represent new concepts, without modifying those types. This technique is important for reusing existing structures, while maintaining compatibility with the current usage. Swift supports retroactive modeling through the use of extensions. Extensions enable you to add new functionality to existing types, without the need to have access to the original source code. Swift extensions are similar to categories in Objective-C, and can be used to extend a class, struct, enum,… [continue]
Swift Essentials: Closures
Most, if not all, programming languages have a construct for defining reusable sets of instructions. In Swift that construct is a closure. A closure is a self-contained chunk of code that is used to perform a specific task. The most basic type of closure is a named closure, which is more commonly known as a function. Functions (Named Closures) Every function is defined using the func keyword followed by the function name. The function name should describe the task that… [continue]
Swift Essentials: Optionals
Swift is designed to be safer than C-based programming languages. For example, Swift is a type safe language, that uses compile-time type checking to help catch errors early in the development process. And Swift uses type inference to enable the compiler to deduce types, requiring far fewer explicit type declarations. Another language feature that enhances safety is the optional. When you declare an instance in Swift, that instance must have a valid value for its type. If that instance can potentially be nil, then… [continue]