Cocoacasts https://cocoacasts.com If you're looking for a quick fix, then I don't have anything to offer you. If you want to become a better Swift developer, then have a seat and let me share with you what I know. en-us Sun, 29 Jan 2023 02:11:04 +0000 Sun, 29 Jan 2023 02:11:04 +0000 40 Is This the Best Mouse for Programmers? While I don't typically write reviews on Cocoacasts, the Logitech MX Master 3S deserves one as it has been a loyal companion of mine for many years. Like most programmers, I'm picky about the equipment I work with. Ideally, the tools I work with should be invisible, allowing me to focus on getting work done. In this post, I hope I can convince you that the Logitech MX Master 3S is the best mouse for programmers like myself. Wed, 18 Jan 2023 06:30:00 +0000 https://cocoacasts.com/best-mouse-for-programmers https://cocoacasts.com/best-mouse-for-programmers Defining Properties We still use string literals to define the properties of an event. That is something I would like to change in this episode. The Journey enum defines the list of events the API supports. We take a similar approach for the properties of an event. Wed, 4 Jan 2023 06:30:00 +0000 https://cocoacasts.com/building-type-safe-apis-in-swift-defining-properties https://cocoacasts.com/building-type-safe-apis-in-swift-defining-properties Creating Events In this episode, we focus on the analytics API itself. We define a single entry point for analytics and remove some of the string literals at the call site. The resulting API is surprisingly small, intuitive to use, and easy to extend. Wed, 28 Dec 2022 06:30:00 +0000 https://cocoacasts.com/building-type-safe-apis-in-swift-creating-events https://cocoacasts.com/building-type-safe-apis-in-swift-creating-events Eliminating Stringly Typed Code Swift has a lot to offer, but it isn't always obvious how to get the most out of the language. I often see developers compromise the APIs they build by using stringly typed code while Swift provides features that make stringly typed code unnecessary. In this series, I show you how a handful of simple patterns and techniques can help you build APIs that are type-safe, elegant, and intuitive to use. Wed, 21 Dec 2022 06:30:00 +0000 https://cocoacasts.com/building-type-safe-apis-in-swift-eliminating-stringly-typed-code https://cocoacasts.com/building-type-safe-apis-in-swift-eliminating-stringly-typed-code Adding Locations The locations view displays a static list of locations. While that has been useful to implement the user interface of the locations view, in the next few episodes we focus on adding locations. Wed, 5 Oct 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-adding-locations https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-adding-locations Displaying Weather Data in the Forecast View In this episode, we focus on the forecast view, a subview or child view of the location view. The forecast view displays the temperature for each day of the weather forecast. Let's add a few more details to the items of the vertical grid. Wed, 28 Sep 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-displaying-weather-data-in-the-forecast-view https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-displaying-weather-data-in-the-forecast-view Displaying Weather Data in the Location View In this episode, we display weather data in the location view. Remember that the location view displays two subviews or child views, the current conditions view and the forecast view. These views are responsible for displaying the weather data for a location. Wed, 21 Sep 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-displaying-weather-data-in-the-location-view https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-displaying-weather-data-in-the-location-view Populating the Location View In this episode, we implement the location view. The user can navigate to the location view by tapping a location in the locations view. The location view displays the current weather conditions at the top and a forecast at the bottom. Wed, 14 Sep 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-populating-the-location-view https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-populating-the-location-view Displaying Weather Data In the previous episode, we wrote quite a bit of code that we wouldn't have written if we were building a SwiftUI application without the Model-View-ViewModel pattern. This is fine since we have laid a foundation we can take advantage of in the next few episodes. In this episode, we continue to build out the user interface of the locations view by displaying weather data. Wed, 7 Sep 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-displaying-weather-data https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-displaying-weather-data Populating the Locations View With the project set up, we can focus on adding features to the weather application we are building. In this episode, we populate the locations view. Wed, 31 Aug 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-populating-the-locations-view https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-populating-the-locations-view Setting Up the Project In this episode, we set up the project for Thunderstorm, the weather application we build in this series. We make a few changes to prepare the project for the MVVM pattern. Wed, 24 Aug 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-setting-up-the-project https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-setting-up-the-project Meet Thunderstorm In the past few episodes, you learned how the Model-View-ViewModel pattern can transform a project and its architecture. In the remainder of this series, we build an application from scratch. Through that process, you deepen your understanding of the MVVM pattern and how it affects a project's architecture, testability, and structure. Wed, 17 Aug 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-meet-thunderstorm https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-meet-thunderstorm Type Safety with Enums Enums have a wide range of applications and some of them are not obvious until you learn about them. In this episode of Writing Elegant Code with Enums, you learn how you can use enums to increase type safety and eliminate stringly typed code. Wed, 10 Aug 2022 06:30:00 +0000 https://cocoacasts.com/writing-elegant-code-with-enums-type-safety-with-enums https://cocoacasts.com/writing-elegant-code-with-enums-type-safety-with-enums Stringly Typed Code A magic number is a number that is used in code without much meaning due to the lack of context. Magic numbers are considered an anti-pattern because they make code harder to read, understand, and maintain. Refactoring code that uses magic numbers is also more risky and can be time-consuming. Wed, 3 Aug 2022 06:30:00 +0000 https://cocoacasts.com/code-smells-in-swift-stringly-typed-code https://cocoacasts.com/code-smells-in-swift-stringly-typed-code Hiding the Model Layer Remember that a view should be dumb. It doesn't care what it displays. The notes view doesn't match that description. It can access the array of notes through its view model. That is a code smell and something we need to change. You learn how to do that in this episode. Wed, 27 Jul 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-hiding-the-model-layer https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-hiding-the-model-layer Fixing Code Smells In the previous episode, we solved some of the problems we discussed earlier in this series. At the same time, we introduced a few code smells. We address those code smells in this episode. Wed, 20 Jul 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-fixing-code-smells https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-fixing-code-smells A Solution In the previous episode, I highlighted a few problems a typical SwiftUI application can suffer from. These problems can be resolved in several ways. In this series, we explore how the Model-View-ViewModel pattern solves these problems. The goal is to put the views of the application on a diet and decouple view logic from business logic. Wed, 13 Jul 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-a-solution https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-a-solution Suspending and Resuming In the previous episode, you learned about the benefits of Swift Concurrency and what problem it solves. Swift Concurrency solves several more problems, which we discuss later in this series. In this episode, we take a peek under the hood to understand how Swift Concurrency does its magic. Wed, 6 Jul 2022 06:30:00 +0000 https://cocoacasts.com/mastering-swift-concurrency-suspending-and-resuming https://cocoacasts.com/mastering-swift-concurrency-suspending-and-resuming The Problem With this and the next episode, I want to make sure you choose for the Model-View-ViewModel pattern for the right reasons. I want to avoid that you adopt MVVM in a project because you were told it is a sound architecture or because you think you need view models if you use SwiftUI. In this episode, I want to highlight a few problems a typical SwiftUI application can suffer from. Those problems can be resolved by the Model-View-ViewModel pattern with relative ease. Wed, 29 Jun 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-the-problem https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-the-problem Meaningless Fallback Values You're probably familiar with Swift's nil-coalescing operator. The nil-coalescing operator returns a fallback value if the statement on its left doesn't produce a value. I use it quite often and there is nothing inherently wrong with it. The code smell we discuss in this episode of Code Smells in Swift is the value the statement on the right of the nil-coalescing operator produces, the fallback value. Wed, 22 Jun 2022 06:30:00 +0000 https://cocoacasts.com/code-smells-in-swift-meaningless-fallback-values https://cocoacasts.com/code-smells-in-swift-meaningless-fallback-values How to Encode Null Using JSONEncoder? The JSONDecoder and JSONEncoder classes make working with JSON a breeze. The true star is Codable, though. Codable, a type alias for Decodable & Encodable, is flexible and provides a lot of functionality for very little effort. From time to time, you run into a situation that makes you scratch your head. This episode covers one such situation. How do you encode null using JSONEncoder? The good news is that the solution is fairly straightforward. Wed, 15 Jun 2022 06:30:00 +0000 https://cocoacasts.com/tips-and-tricks-how-to-encode-null-using-jsonencoder https://cocoacasts.com/tips-and-tricks-how-to-encode-null-using-jsonencoder What Problem Does Swift Concurrency Solve Before I show you how to adopt Swift Concurrency in a project, I want to take a moment to show you why native support for concurrency was added to the language. Let's take a look at an example to better understand what problem Swift Concurrency solves. Wed, 8 Jun 2022 06:30:00 +0000 https://cocoacasts.com/mastering-swift-concurrency-what-problem-does-swift-concurrency-solve https://cocoacasts.com/mastering-swift-concurrency-what-problem-does-swift-concurrency-solve Understanding the Core Concepts Before we take a look at Swift Concurrency, it is important that you become familiar with a few core concepts that relate to Swift Concurrency. While you may already be familiar with some of them, this episode should take away any doubt or confusion you have. Wed, 1 Jun 2022 06:30:00 +0000 https://cocoacasts.com/mastering-swift-concurrency-understanding-the-core-concepts https://cocoacasts.com/mastering-swift-concurrency-understanding-the-core-concepts Are You Writing Unit Tests Swift is a type-safe, statically typed language, which means that the compiler checks your code for type errors at compile time. Put simply, you are not able to release your application if your code contains even a single type error. That is a good thing. Wed, 25 May 2022 06:30:00 +0000 https://cocoacasts.com/mastering-unit-testing-in-swift-are-you-writing-unit-tests https://cocoacasts.com/mastering-unit-testing-in-swift-are-you-writing-unit-tests What Is Type Erasure Swift is a type-safe, statically typed language, meaning that each value has a type and the compiler performs type checking at compile time. Your code won't compile if it contains type errors. The benefit is that common bugs are caught early by the compiler. Swift's type system is strict and that also has its drawbacks. It makes the language less dynamic compared to other languages you may be familiar with, such as Ruby and JavaScript. Wed, 18 May 2022 06:30:00 +0000 https://cocoacasts.com/understanding-type-erasure-in-swift-what-is-type-erasure https://cocoacasts.com/understanding-type-erasure-in-swift-what-is-type-erasure Views, Models, and View Models If you have been reading or watching Cocoacasts for some time, then you know that the Model-View-ViewModel pattern is one of my preferred patterns for architecting applications. I have been using MVVM for several years in various projects and it continues to prove its value in every project I work on. Wed, 11 May 2022 06:30:00 +0000 https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-views-models-and-view-models https://cocoacasts.com/mastering-model-view-viewmodel-with-swiftui-views-models-and-view-models Creating Builders with Enums I love working with enums in Swift. While that may sound odd, I feel enums are undervalued and developers miss out on some of their nicer applications, especially when combined with associated values. Wed, 4 May 2022 06:30:00 +0000 https://cocoacasts.com/writing-elegant-code-with-enums-creating-builders-with-enums https://cocoacasts.com/writing-elegant-code-with-enums-creating-builders-with-enums Type Safety in Swift Types are a fundamental aspect of the Swift language. Swift's type system was pretty easy to understand in the early days. As the language matured, the type system gained in complexity to increase its flexibility. That flexibility came at a cost, though. Today's type system is pretty advanced and some aspects make your head spin. Wed, 27 Apr 2022 06:30:00 +0000 https://cocoacasts.com/mastering-types-in-swift-type-safety-in-swift https://cocoacasts.com/mastering-types-in-swift-type-safety-in-swift Working in a Multithreaded Environment In the early days of the Swift language, it took just a few hours to learn the fundamentals of the language. I'm sorry to say that that is no longer true. Swift has become richer and, as a result, more complex. The language has become much more capable, but those capabilities come at a price, increased complexity. Wed, 20 Apr 2022 06:30:00 +0000 https://cocoacasts.com/mastering-swift-concurrency-working-in-a-multithreaded-environment https://cocoacasts.com/mastering-swift-concurrency-working-in-a-multithreaded-environment A Few More Unit Tests You learned in the previous episode that it is fine to ignore some of the gaps Xcode finds in your test suite. Even though the goal isn't to fill every gap, we need to write a few more unit tests for the APIClient class. Wed, 13 Apr 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-a-few-more-unit-tests https://cocoacasts.com/building-a-modern-networking-layer-in-swift-a-few-more-unit-tests Catching Bugs with Unit Tests Unit tests are very useful for testing edge cases. We explored that in the previous episode. Because bugs sometimes hide in edge cases, unit tests can help you track down hard to find bugs. This is only true if the unit tests you write are sound and cover your code. Wed, 6 Apr 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-catching-bugs-with-unit-tests https://cocoacasts.com/building-a-modern-networking-layer-in-swift-catching-bugs-with-unit-tests Writing Unit Tests for Edge Cases One of the key benefits of a robust test suite is its ability to quickly and easily test scenarios that are uncommon or hard to reproduce. In this and the next episode, we write unit tests for several scenarios that are difficult to test manually. Manual testing has value, but it is time-consuming and it can be tedious to test edge cases. Wed, 30 Mar 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-unit-tests-for-edge-cases https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-unit-tests-for-edge-cases Writing the Wrong Unit Tests Step by step we are increasing the code coverage of the APIClient class. In the previous episode, you learned how to use the APIs the APIClient class exposes to unit test its private methods. Remember that the goal isn't to unit test the private methods of the APIClient class. The goal is to increase the code coverage of the APIClient class. We have a few more unit tests to write. Wed, 23 Mar 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-the-wrong-unit-tests https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-the-wrong-unit-tests Writing Unit Tests for Private Methods Code coverage is a very helpful tool to detect gaps in a test suite. In the previous episode, we enabled code coverage to learn how to write better unit tests for the APIClient class. The coverage report shows that the episodes() method is fully covered. It also reveals that the private request(_:) method lacks coverage. Even though the request(_:) method is private to the APIClient class, we can write unit tests to indirectly test it and increase coverage of the APIClient class. That is the focus of this episode. Wed, 16 Mar 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-unit-tests-for-private-methods https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-unit-tests-for-private-methods Enabling Code Coverage to Find Gaps Are the unit tests we wrote for the /api/v1/episodes endpoint sufficient? How many unit tests should we write? These are some of the questions we answer in this episode. Developers often struggle with unit testing because they aren't sure when enough is enough. As a developer, you want a simple, straightforward answer. The good news is that code coverage can provide that answer. Wed, 9 Mar 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-enabling-code-coverage-to-find-gaps https://cocoacasts.com/building-a-modern-networking-layer-in-swift-enabling-code-coverage-to-find-gaps Writing Readable and Maintainable Unit Tests We can drastically improve the unit tests we wrote in the previous episodes. Even though the unit tests pass without issues, string literals and code duplication are subtle hints that we should take another look at the unit tests we wrote. At the end of this episode, we have a framework in place that we can use for the remaining unit tests of the APIClient class. Wed, 2 Mar 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-readable-and-maintainable-unit-tests https://cocoacasts.com/building-a-modern-networking-layer-in-swift-writing-readable-and-maintainable-unit-tests Stubbing the Cocoacasts API To create a robust test suite, we need to be in control of the environment the test suite runs in. That includes being in control of the requests the application sends to the Cocoacasts API. We don't want the application to hit the Cocoacasts API when the test suite is run. In this episode, I show you how to stub the Cocoacasts API using the OHHTTPStubs library. Tue, 22 Feb 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-stubbing-the-cocoacasts-api https://cocoacasts.com/building-a-modern-networking-layer-in-swift-stubbing-the-cocoacasts-api Unit Testing Asynchronous Code Even though the unit test we wrote in the previous episode passes, we quickly discovered that it gives us a false sense of confidence. The unit test passes because it is executed synchronously. To unit test the networking layer, we need to replace the synchronous unit test with an asynchronous unit test. Let me show you how that works. Tue, 15 Feb 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-unit-testing-asynchronous-code https://cocoacasts.com/building-a-modern-networking-layer-in-swift-unit-testing-asynchronous-code Unit Testing the Networking Layer The networking layer we are building is nearing completion. We added support for most endpoints of the Cocoacasts API and, later in this series, we add support for refreshing an access token using a refresh token. In the next few episodes, we focus on unit testing the networking layer. Tue, 8 Feb 2022 06:30:00 +0000 https://cocoacasts.com/building-a-modern-networking-layer-in-swift-unit-testing-the-networking-layer https://cocoacasts.com/building-a-modern-networking-layer-in-swift-unit-testing-the-networking-layer How to Access Build Settings in Swift A build configuration defines the build settings that are used to build the product of a target. As the name suggests, build settings are used to build the product of a target. Even though you don't commonly access build settings in Swift, it can be useful to keep configuration and implementation separate. It is a pattern I have been using for several years. In this episode, I show you how to access build settings in Swift. Tue, 1 Feb 2022 06:30:00 +0000 https://cocoacasts.com/cocoa-fundamentals-how-to-access-builds-settings-in-swift https://cocoacasts.com/cocoa-fundamentals-how-to-access-builds-settings-in-swift