Weekly Comment
Last week was a busy one for Apple. They released the MacBook Air equipped with the M3 chip, launched iOS 17.4 allowing European Union users to access third-party app stores, expressed dissatisfaction with Spotify and the EU on their official website, and temporarily suspended Epic's developer account (which is reportedly soon to be restored). Additionally, Apple launched Xcode 15.3, bringing with it the update to Swift 5.10.
Among these updates, the release of Swift 5.10 stands out as the most significant for developers. The concurrency model was initially introduced in Swift 5.5, incorporating features like async/await
, actor
, and structured concurrency. Swift 5.7 then introduced Sendable
, establishing a foundation for ensuring type safety in concurrent environments. Now, Swift 5.10, when the full concurrency checking option is enabled, mandates complete data isolation enforced throughout the language, ensuring safety through compile-time checks.
Recompiling my existing project code with Swift 5.10, I mainly encountered issues related to the newly introduced strict concurrency for global variables feature, particularly warnings related to type properties. Developers should now enable strict concurrency checking as soon as possible to prepare for the upcoming versions of Swift.
Soon, social media might be flooded with screenshots of various errors and warnings in Xcode. Hopefully, everyone can approach this potentially "painful" transition with a calm mindset, as it is an essential step forward in enhancing code quality and application stability.
Previous Issue|Newsletter Archive
If you find this weekly newsletter or my blog helpful, I would greatly appreciate your consideration of making a donation through Patreon, or Buy Me a Coffee.
Originals
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.
Over the past three weeks, I have explored through three articles the principles and structures of Swift Predicate, as well as the considerations for handling optional values in SwiftData predicates. Through a period of practical application, Swift Predicate has had a profound impact on me. It not only plays a role within specific frameworks, but I also foresee its extensive application in many more scenarios involving data logic reasoning in future development processes.
Recent Selections
SwiftUI-First Architecture
Since the advent of SwiftUI, finding the most suitable architecture has become a widely discussed topic. Mike Apurin, after experimenting with traditional framework patterns and finding them lacking, advocated for a "SwiftUI-first" architectural approach. This concept champions the full utilization of tools and features provided by SwiftUI, such as @State
and @Environment
, instead of rigidly fitting them into conventional architectural patterns. The SwiftUI-first architecture he endorses emphasizes flexibility and extensibility, encouraging developers to fully leverage the latest functionalities of SwiftUI and adapt the architecture flexibly according to the specific needs of their projects, thereby achieving optimal application performance.
High Performance Swift Apps
Developing an application is just the first step; the key is to ensure it operates securely and efficiently. In this article, Jacob Bartlett thoroughly elucidates the complete process of optimizing the performance of a new application. Through specific code improvements, analysis with the Instruments tool, and performance comparison data, the article comprehensively demonstrates how various optimization measures can significantly enhance application performance. These optimizations ultimately ensure that the app efficiently generates 2FA codes and maximizes the use of the device's multi-core CPU, thereby providing faster processing speeds and an optimized user experience. Although articles introducing specific optimization techniques are numerous, content like this that deeply analyzes the entire application optimization process is relatively rare and of great reference value to developers.
Pitfalls and solutions when building Metal Shaders for Core Image Kernel
As the developer of the professional photography software PhotoCam, JuniperPhoton holds strict standards for the efficiency and quality of image processing. In this article, he delves into the challenges and corresponding solutions encountered while using the Core Image framework in conjunction with Metal shaders for image processing. The article not only provides practical technical details but also reveals how to avoid some common issues. For developers who wish to deeply understand the combined application of Core Image and Metal, this is undoubtedly an invaluable reference.
Swift 5.10
Recently, with the release of Xcode 15.3, Swift 5.10 was officially launched. This version introduces a complete data isolation mechanism in the concurrency language model, laying a solid foundation for the upcoming Swift 6 version. Michael Tsai has compiled comments and summaries from various experts on the new features of Swift 5.10, covering a summary of the core functionalities and their potential impact on the future.
As discussed in the 20th issue of the weekly, developers should prepare for Swift 6 as early as possible. Although I had already enabled the complete concurrency check before, upgrading to Xcode 15.3 still brought up some new issues in the existing project code. The earlier these issues are resolved, the smaller the challenges we will face in the future.
Migrating my SwiftUI App to VisionOS in 2 Hours
In this article, Cihat Gündüz elaborately narrates how he successfully migrated his SwiftUI app, CrossCraft, to visionOS within two hours. Although condensing the migration time to two hours might sound exaggerated, Cihat genuinely demonstrated this achievement by live streaming the entire process and sharing videos. This experience shows us that if your app already runs smoothly on iPadOS and macOS, then adapting it to visionOS will be relatively straightforward. This article not only documents the detailed migration steps but also offers valuable advice and guidance for developers who wish to swiftly transition their apps to visionOS.
肘子的话
上周对苹果公司而言是相当忙碌的一周。他们发布了搭载 M3 芯片的 MacBook Air、推出 iOS 17.4 版本让欧盟用户可以接入第三方应用商店,并且在官网上对 Spotify 和欧盟表达了不满,同时还暂停了 Epic 的开发者账户(据悉将很快恢复)。此外,苹果还发布了 Xcode 15.3 版本,并随之带来了 Swift 5.10 的更新。
在这众多更新中,对开发者而言最重大的莫过于 Swift 5.10 的发布。并发模型最早在 Swift 5.5 版本中引入,包括了 async/await
、actor
和结构化并发等特性。随后的 Swift 5.7 版本引入了 Sendable
,作为确保类型在并发环境中线程安全的基础概念。而今,Swift 5.10 在启用完整并发检查选项后,将强制在语言的所有方面实施完全的数据隔离,以编译时检查为手段确保安全。
我在使用 Swift 5.10 重新编译现有项目代码时,主要遇到的问题源于新引入的 strict concurrency for global variables 机制,尤其是对类型属性的警告。现在开发者们应该尽早启用严格的并发检查,为接下来的 Swift 版本做好准备。
可能很快,社交媒体上就会充斥着关于 Xcode 中各种错误和警告的截图。希望大家能以平和的心态面对这个或许“痛苦”的转变过程,毕竟这是向前迈进、提升代码质量和应用稳定性的必经之路。
如果您发现这份周报或我的博客对您有所帮助,可以考虑通过 爱发电,Buy Me a Coffee 支持我的创作。
原创
如何为 SwiftData 动态的构建复杂的谓词
NSCompoundPredicate
让开发者能够将多个 NSPredicate
对象组合成一个复合谓词。这一机制特别适用于那些需要基于多重判断标准进行数据过滤的场景。然而,在 Swift 重构的新 Foundation 框架中,缺失了与 NSCompoundPredicate
相对应的直接功能,这一变化对希望利用 SwiftData 构建应用的开发者造成了不小的挑战。本文旨在探索如何在当前的技术条件下,利用 PredicateExpression
,动态地构建出符合 SwiftData 需求的复杂谓词。
在过去的三周里,我通过三篇文章探讨了 Swift Predicate 的原理和结构,以及如何 在 SwiftData 中处理 Predicate 可选值 的相关注意事项。通过一段时间的实践,Swift Predicate 对我产生了深远的影响。它不仅在特定框架中发挥作用,我预见在未来的开发过程中,Swift Predicate 将在更多数据逻辑判断的场景中广泛应用。
近期推荐
SwiftUI-First Architecture
自 SwiftUI 问世以来,寻找最合适的架构便成为了广受关注的议题。Mike Apurin 在尝试传统框架模式后发现这些方法未能满足他的需求,因此他倡导了一种 “SwiftUI-first” 的架构理念。这一理念主张充分利用 SwiftUI 所提供的工具与特性,例如 @State
和 @Environment
,而不是将其硬套在传统的架构模式上。他所推崇的 SwiftUI-first 架构,强调了灵活性与可扩展性,鼓励开发者充分利用 SwiftUI 的最新功能,并根据各自项目的需求灵活调整架构,以达到最佳的应用表现。
High Performance Swift Apps
开发应用只是第一步;关键在于确保其运行安全且高效。Jacob Bartlett 在本文中透彻阐述了他对新应用的性能优化的完整流程。借助具体的代码改善、Instruments 工具分析以及性能对比数据,文章全方位展示了各项优化措施如何显著提升应用性能。这些优化最终确保了应用能高效生成 2FA 代码,并最大化地利用了设备的多核 CPU,以此提供更快的处理速度和优化的用户体验。虽然介绍特定优化技巧的文章不在少数,但像本文这样对整个应用优化过程进行深入分析的内容却相对罕见,对开发者而言具有极高的参考价值。
Pitfalls and solutions when building Metal Shaders for Core Image Kernel
作为专业拍照软件 PhotoCam 的开发者,JuniperPhoton 对图像处理的效率和质量持有严格的标准。他在本文中深入分析了使用 Core Image 框架搭配 Metal 着色器进行图像处理时所面临的挑战及其相应的解决策略。文章不只是提供了实际的技术详情,还揭示了如何规避一些常见的问题,对于那些希望深入理解 Core Image 与 Metal 联合应用的开发者来说,这无疑是极具价值的参考资料。
Swift 5.10
近日,随着 Xcode 15.3 的发布,Swift 5.10 正式面世。该版本在并发语言模型中引入了完全数据隔离机制,为即将发布的 Swift 6 版本奠定了坚实基础。Michael Tsai 收集了来自不同专家对 Swift 5.10 新特性的点评和综述,涉及了核心功能的总结及其对未来的潜在影响。
正如 第20期周报 所讨论,开发者们应尽早为 Swift 6 做准备。虽然我在之前已经启用了完整的并发检查,但升级到 Xcode 15.3 后,现有项目代码还是遇到了一些新问题。解决这些问题越早,将来面临的挑战就越小。
Migrating my SwiftUI App to VisionOS in 2 Hours
在这篇文章里,Cihat Gündüz 详细讲述了他如何在两小时内,将他的 SwiftUI 应用 CrossCraft 成功迁移到 visionOS 上。虽然将迁移时间压缩到两小时可能听起来有些夸张,但 Cihat 通过直播整个过程并分享视频,实实在在地展示了这一成就。这一经历向我们展示,若你的应用已经在 iPadOS 和 macOS 上运行良好,那么将其适配到 visionOS 相对来说会简单许多。本文不仅记录了详细的迁移步骤,还为想要迅速将自己的应用转移到 visionOS 的开发者们提供了宝贵的建议和指南。