Weekly Comment
Recently, the Swift community announced the release process for Swift 6. Starting March 15, 2024, the release/6.0 branch will be branched off from the main branch, marking a significant acceleration in the Swift 6 project. It is anticipated to be released alongside the new version of Xcode in the second half of this year.
Swift 6 will introduce a stricter concurrency code checking mechanism, which will result in many of the current codes being unable to compile under Swift 6's language mode. To provide developers with ample time to adapt, the Swift 6 compiler will continue to support the Swift 5 language mode, meaning developers don't have to worry about their existing code not running in Swift 6. To fully leverage the latest features of Swift concurrency, developers are encouraged to enable the strict concurrency check flag (SWIFT_STRICT_CONCURRENCY
) in their projects as early as possible in preparation for Swift 6.
Reflecting on my experience starting to learn the Swift language four years ago, I feel very fortunate because Swift 5 had already been released at that time, achieving ABI stability and thus reducing the learning cost. During the 5.x versions, although the core functionalities of the Swift language had stabilized, the community still added many new features and characteristics, which not only enhanced the functionality and applicability of Swift but inevitably raised the learning threshold for beginners. The plethora of syntactic sugar and new features in Swift can make many beginners feel utterly bewildered when facing complex code.
Things always have two sides: on one hand, developers hope Swift can become more powerful and adapt to more development scenarios and platforms; on the other hand, everyone also wishes it could be easier to learn and use, allowing developers to enjoy the conveniences of new features without additional learning burdens.
The introduction of macros attempts to balance this contradiction to some extent by encapsulating complex functions and features into macros, making it easier for more developers to benefit. However, in the current development environment, the use of custom macros not only significantly increases the project's compile time but also affects Xcode's response when the macros are complex, often leading to frequent errors in code auto-completion and macro expansion. With the release of Swift 6, it is hoped that Xcode will also improve, offering higher efficiency, stability, and contemporary AI-assisted programming capabilities.
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 Handle Optional Values in SwiftData Predicates
SwiftData has revamped the mechanism for creating data models, incorporating a type-safe mode for predicate creation based on model code. As a result, developers encounter numerous operations involving optional values when constructing predicates for SwiftData. This article will explore some techniques and considerations for handling optional values while building predicates.
Recent Selections
How will Apple's changes in the EU affect you?
This article provides an in-depth analysis of the adjustments Apple has made to the App Store in the European Union to comply with the Digital Markets Act (DMA), exploring how these changes affect the promotion and payment models of iOS apps. Gabrielle Earnshaw suggests that developers releasing apps in the EU App Store need to carefully evaluate whether to accept the new business terms. For the vast majority of apps, maintaining the current business model may be the ideal choice. The article also emphasizes that although most apps may not immediately benefit from market alternatives or payment processing changes, these changes undoubtedly bring exciting developments to the iOS ecosystem, with the potential to offer innovation and improvements for developers and consumers alike.
Get the biometric authentication prompt for protected keychain items in the iOS simulator
This article explains how to enable biometric authentication prompts for protected keychain items in the iOS simulator. Due to the absence of the Secure Enclave, a crucial security component that exists only on physical devices and is used for authentication processes such as fingerprint matching, keychain items protected by the kSecAttrAccessControl
attribute cannot trigger biometric authentication prompts in the simulator. To bridge this gap, Marco Eidinger offers a clever solution: explicitly invoking the LAContext.evaluatePolicy(_:localizedReason:)
method before performing keychain item access operations. This ensures that the simulator can simulate biometric authentication prompts, thereby reproducing a user experience similar to that on physical devices in the simulator.
Modding Plugins back into Xcode
In this article, Bryce Pauken delves into how to reintegrate traditional plugin functionalities into Xcode. Before the release of Xcode 8, developers could modify and enhance Xcode's internal functions using third-party plugins by accessing undocumented APIs, granting extensive customization and extension capabilities. This flexibility allowed developers to highly customize their development environment, despite the plugin system not being officially supported or documented. With Apple's increased security measures limiting the execution of third-party code and plugin loading, the traditional way of using plugins gradually came to an end (starting with Xcode 14, Apple completely removed support for old-style plugins). Pauken not only thoroughly explains the key steps to reincorporate old-style plugin functionalities into Xcode but also introduces a tool named XcodePluginLoader, aimed at swiftly restoring plugin support and offering developers a powerful customization tool.
Twitter vs. X
This article analyzes the transformation the iOS client app underwent following Elon Musk's acquisition and renaming of Twitter to X in 2022. Twitter has always been regarded as a mature application, but this purchase and rebranding signified a leap from a state of stable development to a new phase of "acting quickly and embracing disruption." In the approximately six months since Twitter transformed into X, the size of the iOS app increased by 13.3MB. The article provides a thorough discussion on the various changes between the final version of Twitter (v9.54) and X (v10.25) within the iOS app, focusing on changes in app assets, dynamic frameworks, the SPM migration process, issues of resource duplication, and the introduction of the Grok AI chatbot, among other aspects. Despite changes in name and ownership, the author notes that the app did not undergo substantial changes in its overall architecture. The article also points out that by further optimizing duplicated resources within the app, such as icons, it could be possible to reduce the app's size by up to 8MB.
Custom Core Data migrations
Core Data migration involves updating the data model from one version to another. While Core Data can automatically handle this migration in many cases, some complex scenarios require developer intervention, guiding the migration process from the source to the target model by providing an exact mapping model. For particularly complex migration needs, writing a custom migration policy inherited from NSEntityMigrationPolicy
is also necessary. This article, written by Pol Piella, details the implementation methods of custom Core Data migration, including creating mapping model files and custom migration policy classes. The article uses an application that stores music track information as an example, showing how to address data model expansion issues by introducing new entities and relationships and implementing custom migration strategies.
It's worth noting that starting with iOS 17, Apple introduced a new migration mode in Core Data—Stage Migration, similar to the migration method in SwiftData, offering developers more flexibility and control. For more information about this new feature, you can refer to this article.
肘子的话
近日,Swift 社区公布了 Swift 6 的发布流程及预期时间表。自 2024 年 3 月 15 日起,将从主分支中剥离出 release/6.0 分支,标志着 Swift 6 项目正式加速推进,预计将于今年下半年与新版 Xcode 同步发布。
Swift 6 将引入更为严格的并发代码检查机制,这将导致在 Swift 6 的语言模式下很多当前的代码将无法正常编译。为了给开发者提供更充裕的适配时间,Swift 6 编译器将继续支持 Swift 5 的语言模式,这意味着开发者无需担心现有代码无法在 Swift 6 中运行。为了确保代码能够充分利用 Swift 并发性的最新特性,建议开发者尽早在项目中启用严格并发检查标志(SWIFT_STRICT_CONCURRENCY
),为适配 Swift 6 做准备。
回顾自己四年前开始学习 Swift 语言的经历,感到非常幸运,因为那时 Swift 5 已经发布,实现了 ABI 的稳定性,从而降低了学习成本。在 5. x 版本期间,尽管 Swift 语言的核心功能已经稳定,但社区仍然为其增添了众多新功能和特性,这既增强了 Swift 的功能性和适用范围,也不可避免地提高了新手的学习门槛。Swift 中众多的语法糖和新特性,使得许多初学者在面对复杂代码时感到十分困惑。
事物总是具有两面性,一方面开发者期望 Swift 能够变得更加强大,适应更多的开发场景和平台;另一方面,大家也希望它能更易于学习和使用,使开发者能够在不增加学习负担的情况下享受到新特性的便利。
宏的引入在一定程度上试图平衡这种矛盾,通过将复杂功能和特性封装成宏,使得更多开发者能够轻松受益。然而,在当前的开发环境中,自定义宏的使用不仅会显著增加项目的编译时间,而且在宏较为复杂时,Xcode 的响应也会受到影响,导致代码自动提示和宏展开功能频繁出错。期待随着 Swift 6 的发布,Xcode 也能同步提升,带来更高的效率、稳定性以及符合时代的 AI 辅助编程能力。
如果您发现这份周报或我的博客对您有所帮助,可以考虑通过 爱发电,Buy Me a Coffee 支持我的创作。
原创
如何处理 SwiftData 谓词中的可选值
由于 SwiftData 更改了数据模型的创建机制,而且谓词创建也采用了基于模型代码的类型安全模式。因此,当开发者在为 SwiftData 构建谓词时会遇到大量的处理可选值的操作。本文将探讨在构建谓词时,处理可选值的一些技巧和注意事项。
近期推荐
How will Apple's changes in the EU affect you?
本文深入分析了苹果公司为符合欧盟数字市场法案(DMA)而在欧盟对 App Store 进行的调整,探讨了这些改动如何影响 iOS 应用的推广及其收费模式。Gabrielle Earnshaw 提出了对于那些在欧盟 App Store 中发布应用的开发者来说,仔细评估是否接受新的商业条款是必要的。对绝大多数应用而言,维持现行的业务模式可能是较为理想的选择。文章还强调,尽管当前大部分应用可能不立即从市场替代方案或支付处理变革中获益,但这些变化无疑为 iOS 生态系统带来了振奋人心的进展,有望为开发商和消费者带来创新与优化。
Get the biometric authentication prompt for protected keychain items in the iOS simulator
本文讲解了如何在 iOS 模拟器上针对受保护的钥匙串项启用生物识别认证提示。由于 Secure Enclave 这一关键的安全组件仅在实体设备上存在,用于执行如指纹匹配等认证过程,其在模拟器中的缺失意味着使用 kSecAttrAccessControl
属性保护的钥匙串项在模拟器上无法触发生物识别认证提示。为弥补这一差异,Marco Eidinger 提供了一种巧妙的解决策略,即在执行钥匙串项读取操作之前,通过显式调用 LAContext.evaluatePolicy(_:localizedReason:)
方法来确保模拟器能够模拟出生物识别认证提示,从而在模拟器中重现与实体设备相似的用户体验。
Modding Plugins back into Xcode
在本篇文章中,Bryce Pauken 深入探讨了如何将传统插件功能重新整合进 Xcode。在 Xcode 8 推出之前,开发者可以通过访问未公开的 API,使用第三方插件来修改和增强 Xcode 的内部功能,赋予了广泛的自定义及扩展能力。这种灵活性允许开发者根据自己的需求高度定制开发环境,尽管这套插件系统并未得到官方的支持与文档说明。随着 Apple 对安全措施的加强,对第三方代码执行和插件加载的限制,传统的插件方式逐渐走向终结( 从 Xcode 14 开始,Apple 彻底移除了对旧式插件的支持 )。Pauken 在文章中不仅详尽地介绍了如何实现将旧式插件功能重新融入 Xcode 的关键步骤,还推出了名为 XcodePluginLoader 的工具,旨在迅速恢复对插件的支持,为开发者提供了一种强大的自定义工具。
Twitter vs. X
本文分析了自 Elon Musk 于 2022 年收购并将 Twitter 重命名为 X 之后,iOS 客户端应用所经历的转变。Twitter 一直被视为一个成熟的应用程序,但这次收购和重命名意味着它从稳定发展的状态跳跃到了一种“快速行动并接受打破常规”的新阶段。在 Twitter 转变为 X 的大约六个月时间里,iOS 应用的体积增加了 13.3MB。文章对 Twitter 的最终版本(v9.54)与 X(v10.25)之间 iOS 应用的各项变化进行了详尽的探讨,重点包括应用中的资产变化、动态框架、SPM 迁移过程、资源重复问题,以及 Grok AI 聊天机器人的引入等方面。尽管名称和所有权发生了变化,作者观察到,从整体架构上看,应用并未经历实质性的变化。文章还指出,通过进一步优化应用中重复的资源(如图标),有可能为应用减少高达 8MB 的体积。
Custom Core Data migrations
Core Data 迁移涉及将数据模型从一个版本更新至另一个版本的过程。虽然在许多情况下 Core Data 能够自动完成这一迁移,但某些复杂场景则要求开发者手动介入,通过提供精确的映射模型来指导从源模型到目标模型的迁移过程。对于特别复杂的迁移需求,还需编写继承自 NSEntityMigrationPolicy
的自定义迁移策略。本文由 Pol Piella 撰写,详细介绍了自定义 Core Data 迁移的实现方法,包括创建映射模型文件和自定义迁移策略类。文章以存储音乐轨道信息的应用为例,展示了如何通过引入新实体和关系以及实施自定义迁移策略来应对数据模型的扩展问题。
值得一提的是,从 iOS 17 起,Apple 在 Core Data 中引入了一种新的迁移模式——阶段迁移(Stage Migration),这与 SwiftData 的迁移方法类似,为开发者提供了更多灵活性和控制力。想了解更多关于这一新特性的信息,可以参考此篇文章。