Starting with Project Refactoring: Sharing Five Swift Modules
Recently, I embarked on the reconstruction of an app that had not received significant updates for four years. During the process, I extracted some functional modules and shared them as open-source projects. This article provides a brief introduction to the libraries I have open-sourced over the past two months.
Comprehensive Guide to Mastering KeyPath in Swift
In the world of Swift, KeyPath is a powerful yet often underestimated feature. Many developers use it inadvertently in their daily programming without fully realizing its potential and importance. This article aims to delve deeply into the functional characteristics of KeyPath, revealing its unique charm in Swift programming, and helping you transform it into a powerful assistant in your development process.
UserDefaults and Observation in SwiftUI: How to Achieve Precise Responsiveness
In SwiftUI, Apple's @AppStorage property wrapper greatly simplifies the process for developers to respond to and modify UserDefaults content within views. However, with the introduction of the Observation framework, new challenges have arisen—Apple has yet to provide a UserDefaults management solution for Observation. This article will explore how to efficiently and conveniently manage data in UserDefaults under the Observation framework and propose a complete and practical solution.
Mastering Data Binning with Swift Charts
Data binning is a commonly used data processing technique that typically divides continuous numerical or temporal data into multiple intervals (which are mostly adjacent and non-overlapping). This method not only covers the entire data range but also provides clear demarcation for data points within each interval. By performing data binning, we can analyze, visualize, and statistically process complex datasets more effectively. This article will explore how to use the advanced APIs provided by Swift Charts to achieve precise and efficient data binning.
Techniques for Automatic Merging of String Catalogs in Multi-Package Monorepos
Announced at WWDC23, Swift introduces an amazing new way for internationalization (i18n) for Swift apps and packages with String Catalogs. This guide covers the basics of getting started with String Catalogs in a new project: WWDC23 String Catalogs. However, it doesn't touch on a use case where there is a single monorepo with multiple Swift packages (and there are no sample projects). In this article, we'll explore how to set up your monorepo so each Swift Package can have its own String Catalog, which gets auto-merged into one when building your host app, without breaking a sweat!
Swifter and Swifty: Mastering the Swift Testing Framework
Since the inception of the Swift language, XCTest has been the preferred testing framework for the majority of Swift developers. However, deeply rooted in Objective-C, its API design heavily borrows from the traditions of that language, failing to fully reflect the modern best practices of Swift programming. In some respects, this has even become a barrier to further development. To overcome these limitations, Apple officially introduced Swift Testing at WWDC 2024—a new testing framework specifically designed for the Swift language. This framework has been integrated into Xcode 16 and positioned as the official testing tool of choice. In this article, we will delve into the features, usage, and unique aspects of the Swift Testing framework, analyzing how it helps developers write test codes faster (Swifter) and more in line with Swift programming habits (Swifty).
Developing Embedded Applications with Swift
In recent years, Swift has gradually shown its potential for cross-platform development. In this article, I will share some of my experiences and attempts at using the Swift language for embedded development on the SwiftIO development board. This article specifically discusses embedded development on MCU (Microcontroller Unit) hardware that does not have a Memory Management Unit (MMU), and does not cover devices like the Raspberry Pi that possess full general-purpose computing capabilities.
New Frameworks, New Mindset: Unveiling the Observation and SwiftData Frameworks
This content was presented at Let's VisionOS 2024. For easier reading, I have simplified the original material and adjusted it for a more formal written expression. The core message of this share is to convey the following central idea: Although these new frameworks were designed to address issues in existing frameworks, we should not be constrained by past experiences and conventions. It is necessary to learn and use these new tools with an open mind and a fresh perspective. Adopting new frameworks should be seen as an excellent opportunity to refactor projects towards greater safety and modernization.
How to Dynamically Construct Complex Predicates for SwiftData
NSCompoundPredicate allows developers to combine multiple NSPredicate objects into a single compound predicate. This mechanism is particularly suited for scenarios that require data filtering based on multiple criteria. However, in the new Foundation framework restructured with Swift, the direct functionality corresponding to NSCompoundPredicate is missing. This change poses a significant challenge for developers who wish to build applications using SwiftData. This article aims to explore how to dynamically construct complex predicates that meet the requirements of SwiftData, utilizing PredicateExpression, under the current technical conditions.
Swift Predicate: Usage, Composition, and Considerations
NSPredicate has always been a powerful tool provided by Apple, allowing developers to filter and evaluate data collections in a natural and efficient way by defining complex logical conditions. Over time, with the continuous maturation and development of the Swift language, in 2023, the Swift community undertook the task of reconstructing the Foundation framework using pure Swift language. In this significant update, a new Predicate feature based on Swift coding was introduced, marking a new stage in data processing and evaluation. This article aims to explore the usage, structure, and key considerations of Swift Predicate in practical development.
A Deep Dive Into Observation: A New Way to Boost SwiftUI Performance
This article will comprehensively and thoroughly explore the Observation framework in a Q&A format, including its reasons for creation, usage methods, workings, and precautions.
Unified Resource Management in Multi-package Projects
With the continuous improvement of SPM (Swift Package Manager) functionality, more and more developers are starting to separate functionality and manage code in their projects by creating multiple packages. SPM itself provides the ability to manage various types of resources in packages (including localized resources), but it is mainly limited to using these resources within the package and it is difficult to share resources. In cases where multiple targets need to call the same resource, the original method is difficult to cope with. This article will introduce a method for uniformly managing resources in a project with multiple SPM packages.
Collaboration between Combine and async/await
This article will cover the benefits of using Combine and async/await, whether they can work together, and how to make them work together effectively.
AttributedString: Making Text More Beautiful Than Ever
At WWDC 2021, Apple introduced a long-awaited feature for developers - AttributedString. This means that Swift developers no longer need to use the Objective-C-based NSAttributedString to create styled text. This article will provide a comprehensive introduction to it and demonstrate how to create custom attributes.
Apple’s New Formatter API: Comparison of Old and New and How to Customize
This article will introduce how to create a Formatter that complies with the new API, offering readers a different perspective to understand the design mechanism of the new Formatter API; it also compares the new and old versions of the API.
Rebuilding SwiftUI's Redux-like State Container with Async-Await
This article explains how to refactor SwiftUI's state container code using the Async/Await feature in Swift 5.5
Setting Up a Swift Development and Debugging Environment on Linux
Since Swift announced its open-source status and Linux support in 2015, more than 5 years have passed. Despite Swift's rapid development in the initial years, it wasn't widely accepted in the Linux community. This situation was due to several reasons, including issues with language stability and insufficient support for Linux, lacking attractive foundational and third-party libraries, and a shortage of high-profile projects.