Making a Lightweight Dependency Container in Swift
Dependency injection is a powerful design pattern in Swift that helps to improve code flexibility, modularity and testability. At its simplest, it involves providing an object with all the resources it needs to function from an external source rather than creating them internally within the object. This approach improves flexibility by making it easier to replace or mock different dependencies without changing the object itself. We ‘inject’ different object in....
Designing and Implementing a Daily Streak System in Swift
Daily streaks are a great way to boost engagement and help to keep users coming back to your app. If you’re unsure of what a streak is, think of apps such as Duolingo. They track how many days in a row a user has opened your app or completed an action such as finishing a quiz. There’s something about watching the number go up every day and trying not to break the streak that really helps with user retention....
How to Trigger an Action When a SwiftUI Toggle Changes
Sometimes we may need to call an action every time a toggle changes instead of relying purely on a state property to keep our toggle up to date. Luckily this is easy to do. An action can be called when a toggle is updated (or any control that takes in a binding, such as a slider) and a state property will be used to display the state in the control....
How to Hide the Tab Bar When Pushing a New View Controller
Sometimes, when using a UITabBarController, you want to push a new view controller without the tab bar. Unfortunately, the default behaviour for a UITabBarController is to show the tab bar no matter how many view controllers have been pushed. Luckily, there is an easy way to elegantly hide the tab bar using the hidesBottomBarWhenPushed property that every view controller has. Example Using Code func moveToNextViewController() { let vc = MyViewController() vc....
How to Start a UIKit Project With No Storyboards
In this guide, you’ll learn how to start a new Xcode project without storyboards. I prefer to take a full programmatic approach to laying out my UI so when I start a new project, I always follow these steps to make sure the storyboard has been properly removed. Delete The Storyboard The first and most obvious step is to actually delete the Main.storyboard file. To do so, right-click on the file in the project navigator and select Delete....
How to Add a Search Bar to a List in SwiftUI
SwiftUI now has the ability to add a search bar in iOS 15. This is done through the searchable() modifier which adds a search bar under your navigation view automatically. To get started, we need our data and a state property to keep track of the search term. @State private var searchTerm = "" private let cats = ["Persian", "Tabby", "Ragdoll", "Sphynx", "Maine Coon"] Next, we need a computed property to filter our data depending on the search term....
How to Install Dependencies With Carthage
Carthage is a dependency manager that makes using third party dependencies in your project easy. It’s slightly different to its more popular counterpart Cocoapods, where instead of creating an XCWordspace file and modifying files, it instead provides you with binary frameworks which can be imported into your project. This may all sound quite confusing but Carthage is really easy to use and hopefully by the end of this guide you should be up and running in no time....
How to Fetch Data From an API in Swift
Introduction Fetching JSON data from a remote API really is the bread and butter of iOS development so it’s important for you to know how to do it in a neat and reusable way. Today, we will be using Codable and URLSession. Oh, and some generics too. We will be using the website https://jsonplaceholder.typicode.com/todos. This handily provides us with some example JSON data that we can use. [ { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }, { "userId": 1, "id": 2, "title": "quis ut nam facilis et officia qui", "completed": false } ] Defining Our Model To get started, we first need to define a model for the data....