Recent articles
Jump to a random postAn extensive guide to sorting Arrays in Swift
Published on: April 7, 2021When you're working with Arrays in Swift, it's likely that you'll want to sort them at some point. In Swift, there are two ways to sort an Array: Through the Comparable implementation for each element in your array By providing a closure to perform a manual/specialized comparison between elements If you have a homogenous array of elements, for example [String], you can rely on String's implementation of Comparable to sort an array of String in some sensible manner. There are two ways to sort an array of Comparable elements: var strings = ["Oh", "Hello", "World", "This", "Is", "An", "Unsorted", "Array"]...
Read more...Splitting a JSON object into an enum and an associated object with Codable
Published on: April 5, 2021Decoding data, like JSON, is often relatively straightforward. For a lot of use cases, you won't need to know or understand a lot more than what I explain in this post. However, sometimes you need to dive deeper into Codable, and you end up writing custom encoding or decoding logic like I explain in this post. In more advanced scenarios, you might need to have an extremely flexible approach to decoding data. For example, you might want to decode your data into an enum that has an associated value depending on one or more properties that exist in your JSON...
Read more...Writing custom JSON encoding and decoding logic
Published on: April 5, 2021The default behavior for Codable is often good enough, especially when you combine this with custom CodingKeys, it's possible to encode and decode a wide variety of JSON data without any extra work. Unfortunately, there are a lot of situations where you'll need to have even more control. The reasons for needing this control are varied. You might want to flatten a deeply nested JSON structure into a single Codable object. Or maybe you want to assign a default value to a property if it's not possible to extract this value from the received JSON data. Or maybe you want...
Read more...Customizing how Codable objects map to JSON data
Published on: April 5, 2021In the introductory post for this series you learned the basics of decoding and encoding JSON to and from your Swift structs. In that post, you learned that your JSON object is essentially a dictionary, and that the JSON's dictionary key's are mapped to your Swift object's properties. When encoding, your Swift properties are used as keys in the encoded JSON output dictionary. Unfortunately, we don't always have the luxury of a 1 to 1 mapping between JSON and our Swift objects. For example, the JSON you're working with might use snake case (snake_case) instead of camel case (camelCase) for...
Read more...An introduction to JSON parsing in Swift
Published on: April 5, 2021Virtually every modern application needs some way to retrieve, and use, data from a remote source. This data is commonly fetched by making a network request to a webserver that returns data in a JSON format. When you're working with Javascript, this JSON data can be easily decoded into a Javascript object. Javascript doesn't have strong typing, so a JSON object in Javascript is really just a JavaScript Object. Objects in Javascript are very comparable to dictionaries in Swift, except they aren't strongly typed and they have a couple of extra features. But that's way beyond what I want to...
Read more...Flattening a nested JSON response into a single struct with Codable
Published on: April 4, 2021Often, you'll want you Swift models to resemble JSON that's produced by an external source, like a server, as closely as possible. However, there are times when the JSON you receive is nested several levels deep and you might not consider this appropriate or needed for your application. Or maybe you're only interested in a couple of fields from the JSON response and these fields are hidden several levels deep in the JSON that's returned by a server. In this post I'll show you how you can use nested containers to decode nested JSON data into a flat struct with...
Read more...Preventing unwanted fetches when using NSFetchedResultsController and fetchBatchSize
Published on: January 18, 2021This article covers a topic that is extensively covered in my Practical Core Data book. This book is intended to help you learn Core Data from scratch using modern techniques and every chapter features sample apps in SwiftUI as well as UIKit whenever this is relevant. When you use Core Data in a UIKit or SwiftUI app, the easiest way to do this is through a fetched results controller. In SwiftUI, fetched results controller is best used through the @FetchRequest property wrapper. In UIKit, a fetched results controller can be conveniently set up to provide diffable data source snapshots for...
Read more...What does “atomic” mean in programming?
Published on: January 6, 2021When you're learning about databases or multithreaded programming, it's likely that you'll come across the term "atomic" at some point. Usually you'll hear the term in the context of an operation. For example, an atomic read / write operation. Or atomic access to a property. But what does this mean? Generally, you can summarize atomic as "one at a time". For example, when accessing or mutating a property is atomic, it means that only one read or write operation can be performed at a time. If you have a program that reads a property atomically, this means that the property...
Read more...10 things iOS developers should focus on in 2021
Published on: January 4, 2021I know. This is a clickbaity title. And yes, I know that this list is not relevant for everybody. I know that not every iOS developer has to learn everything on this list. That said, this list is a list of technologies and skills that I think are either already important, or becoming increasingly important this year. It's a list of technologies and skills that I have learned, plan to learn, or would like to learn this year. It's also a list that hopefully inspires you to broaden your horizons, and learn new things. Or maybe this list inspires you...
Read more...Observing the result of saving a background managed object context with Combine
Published on: December 7, 2020I love posts where I get to put write about two of my favorite frameworks at the moment; Combine and Core Data. When you're working with Core Data, it's common to perform save operations asynchronously using a background context. You could even perform an asynchronous save on the main managed object context. Consider the following method that I added to an object that I wrote called StorageProvider: public extension StorageProvider { func addTask(name: String, description: String, nextDueDate: Date, frequency: Int, frequencyType: HouseHoldTask.FrequencyType) { persistentContainer.performBackgroundTask { context in let task = HouseHoldTask(context: context) task.name = name task.taskDescription = description task.nextDueDate =...
Read more...