Weekly Comment
After several months of studying SwiftData, I only recently adopted it in my project. However, I found that coding efficiency has decreased compared to using Core Data. This is not because SwiftData is difficult to use; in fact, even though SwiftData is developed based on Core Data, to use and understand it correctly, I had to abandon many of the Core Data experiences I previously mastered and try to adopt programming logic more aligned with the design philosophy of SwiftData, leading me to start over several times.
I've noticed that many developers transitioning from Core Data to SwiftData often only take a few days or even less time. This approach can lead to a problem: simply treating SwiftData as a Swift-language version of Core Data and merely converting Core Data code into SwiftData code. This situation does not only occur with the application of SwiftData; in the use of the Observation framework, developers are merely converting code based on ObservableObject
into a new pattern. This method severely weakens the opportunity for structural and conceptual changes brought about by the framework transition, which is a significant regret.
It has been five years since SwiftUI was released, and although the development speed is not fast, Apple has gradually provided developers with a more comprehensive modern programming framework. Currently, the main challenge developers face is a lack of deep understanding and practical experience with this framework. In the process of framework transition, developers should not only be limited to direct code conversion but also need to undergo a shift in thinking and philosophy to truly unlock and utilize the full potential of the new framework.
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
Exploring Property Wrappers in SwiftUI: @UIApplicationDelegateAdaptor, @AccessibilityFocusState, @FocusedObject, @FocusedValue, and @FocusedBinding
In this article, we will explore property wrappers such as @UIApplicationDelegateAdaptor
, @AccessibilityFocusState
, @FocusedObject
, @FocusedValue
, and @FocusedBinding
. These property wrappers cover various functionalities including integration across different framework lifecycles, assistive focus, and management of focused value observations.
Swift property wrappers were born the same year as SwiftUI. SwiftUI takes full advantage of this feature, offering developers a series of property wrappers that greatly simplify the development process. In this series of four articles, we have thoroughly sorted through all the property wrappers provided by SwiftUI up to iOS 17, aiming to help developers use SwiftUI more efficiently and conveniently. We hope this content will provide valuable guidance and assistance to everyone using SwiftUI.
Recent Selections
The Swift Method Dispatch Deep Dive
This article delves into the method dispatch mechanism in the Swift language, aiming to elucidate the mechanism of Swift's function calls and their specific impact on code performance. The author, Jacob Bartlett, employs both detailed and comprehensible approaches to analyze the basic concept of method dispatch and how various types of method dispatch (including static dispatch, table dispatch, and message dispatch) affect code performance. The article further discusses adopting certain strategies, such as using the final
keyword, privatizing methods, and optimizing the entire module, to reduce the frequency of dynamic dispatch, thereby effectively enhancing the execution efficiency of Swift code.
Discovering Swift Algorithms package
Maintained officially by Apple, Swift Algorithms is an open-source library designed to bring a series of efficient, powerful, and practical algorithm extensions to the Swift language. By enriching the collection of algorithms in the Swift standard library, the library greatly facilitates developers in data processing and computation. In this article, Majid Jabrayilov utilizes his extensive practical experience to show us how to leverage various functionalities provided by the Swift Algorithms library, such as binary search, data chunking, filtering, sampling, and element combination, to effectively enhance the efficiency and performance of project development.
Encapsulate and Generalize in Swift
Frequent incorporation of third-party libraries in projects leads developers to extensively use import
statements in their code. In his article, Scott Smith proposes a strategy to alleviate the burden of third-party dependencies in Swift projects: ensuring that each third-party library is imported only once throughout the entire project and avoiding the direct use of dependency names in variables, class names, or any other identifiers. He emphasizes that by encapsulating and generalizing references to third-party libraries, it is possible to prevent the code from being too tightly coupled with specific libraries, a tight coupling that introduces unnecessary complexity and additional work when switching libraries or adjusting functionalities.
The SwiftUI Field Guide
SwiftUI offers a set of rich and powerful layout tools. However, for beginners, it's often difficult to quickly grasp the specific functions and application effects of various layout containers and their parameters merely by reading the documentation. To address this challenge, Chris Eidhof launched "The SwiftUI Field Guide" website. This site uses interactive demonstrations to explain in detail the workings of the SwiftUI layout system, aiming to help developers intuitively master the use of layout tools.
Note: The SwiftUI layout effects displayed on the website are implemented using web technologies, so their behavior may have slight differences from SwiftUI's actual performance. Despite this, these differences do not hinder developers' understanding and learning of the basic functions of layout containers.
A week on with a VisionPro
In this article, Joseph Heck documents his initial experience with using the Apple Vision Pro, including his thoughts and feelings, with the intention of reflecting on the changes the product undergoes after several iterations and updates. Although he encountered some challenges and shortcomings during use, Heck maintains a positive attitude towards the development and application potential showcased by the Apple Vision Pro, especially in areas such as information visualization and software debugging. He is filled with anticipation for future innovations and explorations using the Apple Vision Pro, demonstrating a developer's enthusiasm for exploring the endless possibilities of new technology.
肘子的话
经过几个月对 SwiftData 的研究,我最近才在项目中正式采用了它。然而,我发现与使用 Core Data 相比,编写代码的效率有所下降。这并非因为 SwiftData 难以使用,实际上,尽管 SwiftData 是在 Core Data 的基础上发展而来,但要想正确地使用和深入理解它,我必须放弃许多我以前掌握的 Core Data 经验,尝试采用与 SwiftData 设计哲学更为契合的编程逻辑,这个过程中我不得不几次重新开始。
我注意到,许多开发者在从 Core Data 转换到 SwiftData 的过程中,往往只花费了几天甚至更短的时间。这种做法容易导致一个问题:仅仅将 SwiftData 当作 Swift 语言化的 Core Data,并简单地将 Core Data 的代码转化为 SwiftData 的代码。这种情况不仅仅发生在 SwiftData 的应用中,在使用 Observation 框架时,也有开发者仅仅是将基于 ObservableObject
的代码转换为新的模式。这种做法严重削弱了框架转换带来的结构和思维转变的机会,这是一个巨大的遗憾。
SwiftUI 自发布以来已五年,尽管发展速度并不算快,但苹果公司已经逐步向开发者们提供了一套更为全面的现代化编程框架。当前,开发者所面临的主要挑战,是缺乏对这套框架的深入理解和实践经验。在进行框架转换的过程中,开发者不应仅仅局限于代码层面的直接转换,更为关键的是要进行思维和理念上的转变,这样才能真正发掘并利用新框架的全部潜力。
如果您发现这份周报或我的博客对您有所帮助,可以考虑通过 爱发电,Buy Me a Coffee 支持我的创作。
原创
探讨 SwiftUI 中的属性包装器:@UIApplicationDelegateAdaptor、@AccessibilityFocusState、@FocusedObject、@FocusedValue 和 @FocusedBinding
在本文中,我们将对 @UIApplicationDelegateAdaptor
、@AccessibilityFocusState
、 @FocusedObject
、@FocusedValue
和 @FocusedBinding
等属性包装器进行探讨。这些属性包装器涵盖了不同框架声明周期的整合、辅助聚焦、焦点值观察管理等功能。
Swift 语言的属性包装器与 SwiftUI 诞生于同一年。SwiftUI 充分利用这一功能,为开发者提供了一系列属性包装器,极大地简化了开发过程。在这个系列的四篇文章中,我们详细梳理了截至 iOS 17 时期 SwiftUI 所提供的全部属性包装器,旨在帮助开发者更加高效和便捷地使用 SwiftUI。希望这些内容能对大家在使用 SwiftUI 时提供有价值的指导和帮助。
近期推荐
The Swift Method Dispatch Deep Dive
本文深入探讨了 Swift 语言中的方法派发机制,目的在于阐释 Swift 执行函数调用的机制及其对代码性能的具体影响。作者 Jacob Bartlett 采用了既详细又易于理解的方式,解析了方法派发的基本概念和各类方法派发(包括静态派发、表派发和消息派发)如何影响代码的性能表现。文章进一步讨论了通过采纳某些策略,如使用 final
关键字、方法的私有化以及全模块优化等,来降低动态派发的频率,从而有效提升 Swift 代码的运行效率。
Discovering Swift Algorithms package
由苹果公司官方维护的 Swift Algorithms 是一个开源库,旨在为 Swift 语言带来一系列高效、强大及实用的算法扩展。该库通过丰富 Swift 标准库的算法集合,为开发者在数据处理和计算执行上提供了极大的便利。在这篇文章中,Majid Jabrayilov 利用自己丰富的实践经验,向我们展示了如何借助 Swift Algorithms 库中提供的各种功能,如二分搜索、数据分块、过滤、采样和元素组合等,有效提升项目开发的效率和执行性能。
Encapsulate and Generalize in Swift
在项目中频繁引入第三方库,导致开发者不得不在代码中大量使用 import
语句。Scott Smith 在他的文章中提出了一种策略,以减轻 Swift 项目对第三方依赖的负担:确保整个项目仅单次导入每个第三方库,并避免在变量、类名或任何其他标识符中直接使用依赖库的名称。他强调,通过封装和泛化对第三方库的引用,可以避免代码与特定库过于紧密的绑定,这种紧密绑定会在更换库或调整功能时引入不必要的复杂性和额外工作。
The SwiftUI Field Guide
SwiftUI 提供了一套既丰富又强大的布局工具集,然而,对于初学者来说,仅通过阅读文档往往难以迅速理解各种布局容器及其参数的具体功能和应用效果。针对这一挑战,Chris Eidhof 推出了 ”The SwiftUI Field Guide“ 网站。该网站采用交互式演示的形式详细解释了 SwiftUI 布局系统的运作机理,旨在帮助开发者直观地掌握布局工具的使用。
需要注意的是,网站上展示的 SwiftUI 布局效果是基于 Web 技术实现的,因此其行为可能与 SwiftUI 的实际表现存在细微差别。尽管如此,这些差异并不妨碍开发者对布局容器基础功能的理解和学习。
A week on with a VisionPro
在本文中,Joseph Heck 记录了他初次使用 Apple Vision Pro 的体验和所思所感,目的是为了日后回顾这款产品经过数次迭代更新后的变化。虽然在使用过程中遇到了一些挑战和不足,但 Heck 对于 Apple Vision Pro 展现出的开发和应用潜力持积极态度,尤其是在信息可视化和软件调试这些领域。他对未来利用 Apple Vision Pro 进行创新和探索充满了期待,展现了一位开发者对新技术探索无限可能的热情。