Code Generating Swift Mocks with Sourcery
YASB
by Vadim Bulavin
3y ago
Sometimes it is difficult or even impossible to verify the system under test (SUT) because it depends on other components that cannot be used in the test environment. It can be that they aren’t available (an internet connection), they will not return the results needed (user location), or because executing them would have undesirable side effects (firing an HTTP request) [1]. When you test something, you refer to it as the system under test (SUT). In such a case it is convenient to replace real dependent-on components with their counterparts that we can fully control. This technique is calle ..read more
Visit website
Custom Popup in SwiftUI
YASB
by Vadim Bulavin
3y ago
Popup is a kind of modal that appears in front of app content to provide critical information or ask for a decision. SwiftUI provides many APIs to show alerts, popovers, action sheets, modal sheets. However, none of these allow us to present a custom popup or a snackbar. In this article, let’s build a reusable SwiftUI component for presenting custom popups and snackbars. You can find the complete project on GitHub Popup View Modifier It’s convenient to design the popup as a ViewModifier that can be applied to any view: struct Popup<T: View>: ViewModifier { let popup: T let is ..read more
Visit website
Callable Objects and callAsFunction() in Swift
YASB
by Vadim Bulavin
3y ago
In Swift, any object that has the callAsFunction method can be called like a function. Such objects are named callable. The function-call syntax forwards arguments to the corresponding callAsFunction method [SE-0253]: struct WannabeFunction { func callAsFunction() { print("Hi there") } } let f = WannabeFunction() f() // Hi there There are three ways of invoking callAsFunction() on f: let f = WannabeFunction() f() // Callable sugar f.callAsFunction() // Member function sugar WannabeFunction.callAsFunction(f)() // No sugar The callAsFunction() method obeys all Swift funct ..read more
Visit website
Effective Auto Layout Programmatically in Swift
YASB
by Vadim Bulavin
3y ago
Interface Builder (IB) has been in Xcode going back as far as anyone could recall [1]. It initiated the disputes about developing user interface in code vs IB which continue till nowadays. However, with the release of SwiftUI, Apple has made programmatic layout the mainstream approach. Why hadn’t this happen before? The folklore tells us that constructing UI in code is slower compared to Interface Builder. In this article, I am going to show not only that the opposite is true, but also that programmatic layout is more scalable, maintainable, and future-proof when using the five techniques of E ..read more
Visit website
SwiftUI View Lifecycle
YASB
by Vadim Bulavin
3y ago
Each view undergoes a series of events from its birth to its death, which is referred to as a lifecycle. Understanding it is essential when building apps in SwiftUI. In this article, we will explore the three phases of the SwiftUI view lifecycle. Before we talk about lifecycle, we need to agree on what a view is. Understanding SwiftUI Views To describe what’s displayed onscreen, we create a graph of views. View is a definition of a piece of UI. [1]. SwiftUI uses this definition to create an appropriate rendering. View is a function of a state [2]. To update UI, we don’t mutate the view gra ..read more
Visit website
Unit Testing Asynchronous Code in Swift
YASB
by Vadim Bulavin
3y ago
If you are practicing unit testing, sooner or later you’ll run across testing async code. Writing such tests can be a challenge for the next reasons: False positives and negatives. Async code ends after the unit test does, causing misleading test results. Flakiness. Async code executes with different speed on different machines, bringing a subtle time dependency into a test suite. A common symptom of flakiness is that on your development machine all tests are passing, however, when run on a CI/CD slave, a couple of random tests are failing. Untrustworthy. Because of the flakiness, we stop tru ..read more
Visit website
Working with an Internet Connection on iOS with Swift: Best Practices
YASB
by Vadim Bulavin
3y ago
Networking is an integral part of most iOS applications. A common network-related task is Internet connectivity detection. Typically, it appears in three scenarios: Checking connectivity before firing an HTTP request. Disabling or enabling app features based on network connectivity status. Attaching constraints to network operations, e.g., disabling large file download via cellular. The most popular answers on how to detect network connectivity status on iOS suggest using SCNetworkReachability. In this article, let’s discuss why this solution is less than optimal, and lay out best practices ..read more
Visit website
Infinite List Scroll with SwiftUI and Combine
YASB
by Vadim Bulavin
3y ago
Infinite scrolling is a UX pattern which loads content continuously when users scroll down the screen. An example of an infinite list that you probably already know is Twitter or Instagram feed. In this article, let’s implement an endless list of GitHub repositories using the SwiftUI and Combine frameworks, and the MVVM iOS app architecture pattern. Firing Paginated HTTP Request We begin by adding a network request that fetches repositories using GitHub REST API: In Modern Networking in Swift you can learn how to implement a networking layer from scratch. enum GithubAPI { static let pageSi ..read more
Visit website
Swift Pointers Overview: Unsafe, Buffer, Raw and Managed Pointers
YASB
by Vadim Bulavin
3y ago
Pointers are one of the most complex features in the Swift language. First, the concept of pointers is hard by itself. Second, using them incorrectly could lead to memory leaks and undefined behavior. Third, Swift exposes pointers through a set of thirteen types, which may seem overwhelming. The goal of this article is to put Swift pointers in a systematic way: what they are, when to use, and what you can get from them. What is Memory Memory is a long list of bytes. Every byte has a number, called memory address, and can hold a content. Using memory addresses, we can store things in memory and ..read more
Visit website
SwiftUI Previews at Scale
YASB
by Vadim Bulavin
3y ago
Although SwiftUI previews fulfill their goal of generating dynamic, interactive previews, they come not without a price. SwiftUI previews sit on the boundary of test and production code, therefore, when applied systematically, they inevitably result in test code leaking into production. In this article let’s learn three techniques to help you manage SwiftUI previews at scale: Extracting reusable previews into components. Combining groups of previews into presets. Using Fixture Object pattern to simplify object graph instantiation. This is not an introduction to the subject. We won’t discuss ..read more
Visit website

Follow YASB on FeedSpot

Continue with Google
Continue with Apple
OR