Recent articles
Jump to a random post@preconcurrency usage in swift explained
Published on: May 28, 2024When you enable strict concurrency checks for your existing projects, it’s likely that Xcode will present loads of warnings and/or errors when you compile your project for the first time. In this post, I’d like to take a look at a specific kind of error that relates to code that you didn’t write. The @preconcurrency declaration can be added to: functions types protocols imports Let’s take a look at all of these areas to fully understand how @preconcurrency helps us enable strict concurrency checks even if we can’t update all of our dependencies just yet. @preconcurrency imports To be specific,...
Read more...Programmatic navigation in SwiftUI with NavigationPath and navigationDestination
Published on: May 22, 2024One of the key features that was missing from SwiftUI when it first shipped was a good way to do programmatic navigation. There were some ways to handle this before iOS 16 introduced NavigationPath but it wasn’t very satisfying to use those APIs and they could be rather unreliable at times. To see an example, take a look at this post I wrote about handling deeplinks on iOS 14. In this post, I’d like to revisit programmatic navigation through iOS 16’s NavigationPath API which is a huge leap forward in terms of developer experience and reliability at the same time....
Read more...Turn off sidebar hiding on NavigationSplitView in SwiftUI
Published on: May 21, 2024By default, a NavigationSplitView in SwiftUI will show users an option to toggle the visibility of the sidebar. If you want to prevent this button from showing up so that users will always have to see your sidebar, you can do this by applying the toolbar(removing:) view modifier to your split view's sidebar as follows: NavigationSplitView(sidebar: { ExercisesList() .toolbar(removing: .sidebarToggle) }, detail: { ExerciseDetail(exercise: exercises.first!) }) The downside of doing this is that the button is hidden both in portrait and landscape modes. The result is that landscape users can no longer access your app's sidebar. To fix this you...
Read more...Collections 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...In 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...How to add a privacy manifest file to your app for required reason API usage?
Published on: May 1, 2024Apple has recently introduced a new requirement that makes it so that apps that use certain APIs for Apple's mobile platforms (iOS, iPadOS, tvOS, watchOS) must declare their intended use of certain APIs. This requirement has gone in effect on May 1st which means that any app updates or submissions that don't meet Apple's new requirements will be rejected with a "Missing API Declaration" message also referenced as ITMS-91053. In this post, I'd like to show you how you can add a privacy manifest file to your app so that you can resolve rejections related to ITMS-91053. We'll go over...
Read more...Sometimes, 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...In 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...In 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. Understanding pattern matching The syntax for if case let is somewhat complex. So let’s start with a...
Read more...