Swift fundamentals
What are Optionals in Swift?
Published on: August 12, 2024In an earlier article, I explained how variables are defined in Swift using let and var. Both constants (let) and variables (var) in Swift always have a type; it's what makes Swift a strongly typed language. For example, we could define a String variable like this: // the compiler will know myString is a String var myString = "Hello, world" // we're explicitly telling the compiler that myString2 is a String var myString2: String = "Hello, world" This way of defining variables makes a lot of sense when it's possible to immediately assign a value to our variable. However, sometimes...
Read more...Let and var in Swift explained
Published on: July 12, 2024Virtually every programming language will have some means to define properties; Swift does too. We have two approaches to defining a property in Swift. We can use a var or a let. The code below shows how we can define a var or a let as a member of a class: class Member { let id: UUID var name: String init(name: String) { self.id = UUID() self.name = name } } This class has two properties. One is a let, the other is a var. If you're coming from a Javascript background you might expect that there's a third option...
Read more...How to decide between a Set and Array in Swift?
Published on: May 15, 2024Collections are a key component in any programming language. We often refer to collections as Array or Set but there are several other kinds of collections in programming like String (often a collection of type Character) and ArraySlice (referring to a part of an array). In this post, I’d like to explore two of the most common collection types; Set and Array. We’ll take a look at the key characteristics for each and we’ll explore use cases where we can use each. We’ll cover the following topics: Understanding Array’s key characteristics Understanding Set’s key characteristics Exploring performance considerations Use cases...
Read more...Swift’s “if” and “switch” expressions explained
Published on: May 14, 2024In Swift, we sometimes want to assign a property based on whether a certain condition is true or false, or maybe based on the value of an enum. To do this, we can either make a variable with a default value that we change after checking our condition or we define a let without a value so we can assign a value based on our conditions. Alternatively, you might have used a ternary expression for simple assignments based on a conditional check. Here’s what a ternary looks like: let displayName = object.isManaged ? object.managedName : object.name This code isn’t easy...
Read more...What are enums in Swift?
Published on: May 8, 2024Swift comes with types of objects that we can use to write type declarations. They all have their own distinct features, upsides, and downsides. In this post I’d like to zoom in on the enum type so you can get a sense of what enums are, and when they can be useful. In this post we’ll cover the following topics: Understanding the basics of enums Knowing when an enum should be used Avoiding enum overuse Let's jump right in! Understanding the basics of enums In Swift, enums are values types that are declared using the enum keyword. Every possible value...
Read more...What is defer in Swift?
Published on: April 29, 2024Sometimes, we write code that needs set some state or perform some work at the start of a function and at the end of that same function we might have to reset that state, or perform some cleanup regardless of why we’re exiting that function. For example, you might have a function that creates a new Core Data object and depending on whether you’re able to enrich the object with data from the network you want to exit the function early. Regardless of how and why you exit the function, you want to save your newly created object. Writing our...
Read more...Deciding between a computed property and a function in Swift
Published on: April 26, 2024In Swift, we can use computed properties to derive a value from other values defined on the same object. Being able to do this is super convenient because it means that we don’t have to manually make sure that we update derived properties every time one of the “source” values changed. We’ll just recompute the property every time it’s accessed! If you prefer to learn from video, here's the companion video for this blog post: This is very similar to having a function that takes no arguments and returns a value: struct User { let givenName: String let familyName: String...
Read more...if case let in Swift explained
Published on: April 26, 2024In Swift, we can use the case keyword in multiple places. Most commonly, a case is used in switched but since you’re here, you might have seen a case in combination with an if statement. In this post, we’ll explore different places where we can use the case keyword to perform something called pattern matching in Swift. Pattern matching is a powerful feature of Swift that allows us to perform highly elegant checks to see if a given type matches a certain value. In this post, we’ll explore a specific kind of pattern matching in Swift; the if case let...
Read more...What are lazy vars in Swift?
Published on: April 23, 2024Sometimes when you’re programming you have some properties that are pretty expensive to compute so you want to make sure that you don’t perform any work that you don’t absolutely must perform. For example, you might have the following two criteria for your property: The property should be computed once The property should be computed only when I need it If these two criteria sound like what you’re looking for, then lazy vars are for you. A lazy variable is defined as follows: class ExamResultsAnalyser { let allResults: [ExamResult] lazy var averageGrade: Float = { return allResults.reduce(0.0, { total, result...
Read more...Deciding between a for loop or forEach in swift
Published on: April 23, 2024Swift offers multiple ways to iterate over a collection of items. In this post we’ll compare a normal for loop to calling forEach on a collection. Both for x in collection and collection.forEach { x in } allow you to iterate over elements in a collection called collection. But what are their differences? Does one outperform the other? Is one better than the other? We’ll find out in this post. Using a regular for loop I’ve written about for loops in Swift before so if you want an in-depth look, take a look at this post. A regular for loop...
Read more...