Weekly Comment
Experience is a Double-Edged Sword
A few months ago, I was invited to write a SwiftUI tutorial, which included a section on SwiftData. Recently, when I started preparing this content, I found the difficulty far exceeded my expectations. On one hand, I needed to find a way to express concepts suitable for beginners; on the other hand, I needed to break free from the confusion caused by my Core Data experience in understanding and explaining SwiftData.
Indeed, my experience with Core Data has now become a constraint, hindering my ability to analyze and understand SwiftData from a more modern and forward-looking perspective. I'm striving to extract valuable content from historical experience while avoiding its negative impact on understanding new knowledge. There's been some progress, but it's not fully there yet.
In Jin Yong's novel "The Heaven Sword and Dragon Saber", Zhang Sanfeng teaches Zhang Wuji his self-created Tai Chi in front of enemies. Only when Zhang Wuji claims to have completely forgotten the routines he just learned does Zhang Sanfeng believe he has understood the true essence of the martial art (mastering the "Way" without being restricted by the "Technique"). Although this is a fictional plot, it reveals a truth: extracting the essence from experience without being bound by it is both important and difficult.
When developers face complex layouts and interactions, if they do not deeply understand SwiftUI’s unique layout logic and internal mechanisms, they may be unable to accurately express their needs through these “simple” APIs. In such cases, some developers might mistakenly think SwiftUI’s capabilities are limited, without realizing that the issue lies in their shallow understanding of the framework.
The above is a passage from my article "Common Misconceptions About SwiftUI". Some readers strongly opposed this, often those with UIKit programming backgrounds and limited experience with SwiftUI. They believe SwiftUI is no match for UIKit, with many tasks easily accomplished in UIKit being difficult to implement in SwiftUI. Even some developers experienced with other declarative frameworks find their intuition often at odds with SwiftUI's declarative approach.
I never deny that SwiftUI still has a considerable gap compared to UIKit in terms of feature richness. However, have these developers seriously considered: When learning and using SwiftUI, did they prepare mentally for the shift in thinking? Did they approach SwiftUI with an attitude of accepting and embracing something new? Is your current 'intuition' truly innate, or has it been shaped by other experiences?
Experience is crucial for personal growth, but adhering rigidly to it and being constrained by it will actually hinder us from going further. Experience is a double-edged sword; we must wield it to our advantage, not allow it to turn against us.
Previous Issue|Newsletter Archive
If you appreciate my work and want to promote your product to the Swift and iOS developer community, sponsoring my platform could be an excellent opportunity for you.
Your support through Patreon, or Buy Me a Coffee helps keep this newsletter and blog running. Thank you for finding this content helpful!
Originals
Common Misconceptions About SwiftUI
This article aims to explore several common misconceptions to help developers better understand and utilize SwiftUI. We will dissect these misconceptions, including those about the perceived difficulty of learning SwiftUI, expectations of cross-platform development, the scope of framework capabilities, and misunderstandings about the amount of code involved. By clarifying these concepts, we hope to provide clearer learning directions and usage strategies for SwiftUI developers.
Recent Selections
Magical Particle Effects with SwiftUI Canvas
Canvas is a powerful tool in SwiftUI, allowing developers to draw content in a way that is closer to the underlying layer, significantly improving runtime performance. In this article, Pavel Zak demonstrates how to cleverly use SwiftUI’s Canvas view to create stunning magical particle effects. Notably, all the cool visual effects in the article rely entirely on the SwiftUI framework, showcasing not only excellent visual experiences but also ensuring outstanding performance.
Memory Consumption when Loading UIImage from Disk
Since SwiftUI does not support loading images directly via a path, memory consumption can quickly increase when creating a large number of Image
instances. In this article, Antoine van der Lee delves into the issue of memory management when loading images. He thoroughly analyzes several effective optimization strategies: first, considering whether it is truly necessary to store images in memory; then, discussing how to use UIImage(contentsOfFile:)
to avoid system caching; and finally, proposing how to implement an efficient image caching mechanism to reduce memory usage. Through rich code examples and performance comparisons, Antoine shows how to strike a good balance between application performance and memory consumption.
Swift VS TypeScript Comparison CheatSheet
Luca Ban, with years of experience in TypeScript development, recently started using Swift to develop VisionOS applications. He discovered that although TS and Swift share some similarities in concept, in his current projects, he still needed to find more native (Swifty) ways to use the Swift language. Therefore, he created this quick reference to help developers find equivalent methods between the two languages. This cheat sheet details common method comparisons between TypeScript and Swift, helping developers quickly adapt and improve efficiency. The website also welcomes developers to submit Pull Requests to further enrich the content.
Leveling Up SwiftData Error Handling in Xcode Templates
Although Xcode provides default templates for SwiftData applications, there is still much room for improvement in error handling. Mike Buss proposes a more robust method to handle errors that may occur when creating ModelContainer
. Compared to the default template’s approach of using fatalError()
, which causes the application to crash, the new method offers a better user experience through state management and custom error views.
The method described in this article is not only applicable to SwiftData but can also be applied to any scenario where errors might occur during application initialization.
Async Await in Swift: The Full Toolkit
Starting with a common iOS interview question, Jacob Bartlett provides a comprehensive explanation of the core components in Swift's concurrency toolkit, including async/await, async let, Task, Task group, Actor, MainActor, Sendable, Continuations, AsyncSequence, AsyncStream, and Async Algorithms. For each tool, Jacob not only explains the basic concepts and syntax but also provides practical use cases and code examples, helping readers understand when and where to use these tools most appropriately.
Using @DebugDescription in Xcode 16
In Swift, the CustomDebugStringConvertible
protocol allows developers to customize the way data is described in debug mode to enhance the debugging experience. In this article, Aryaman Sharda introduces the new @DebugDescription
macro in Xcode 16, which takes CustomDebugStringConvertible
a step further by allowing developers to see custom debug descriptions directly in Xcode’s variable inspector without using print
or po
commands. The author points out that clear and informative debug output is crucial for understanding code behavior.
肘子的话
经验是柄双刃剑
几个月前,我受邀撰写一个 SwiftUI 的教程,其中包含一部分关于 SwiftData 的内容。最近当我开始准备这部分内容时,发现难度远超预期。一方面,我需要找到适合初学者的表述方式;另一方面,我需要摆脱 Core Data 对我在理解和表述 SwiftData 时造成的困扰。
是的,我在 Core Data 上的经验如今成为了一种束缚,阻碍了我用更加现代和前瞻的视角分析和理解 SwiftData。我正在努力从历史经验中提取有价值的内容,同时避免其对新知识的理解产生负面影响,虽然有些进展,但还未完全到位。
在金庸的小说《倚天屠龙记》中,张三丰当着敌人的面向张无忌传授了他自创的太极拳。直到张无忌表示他已经彻底忘记了刚才所学的套路,张三丰才认为他理解了该武功的真谛( 掌握了“道”,但并不被“术”所限制 )。尽管这是小说情节,但确实揭示了一个事实:如何从经验中提取本质而不被其束缚,是一件既重要又困难的事。
当开发者面对复杂的布局和交互时,如果不深入理解 SwiftUI 独特的布局逻辑和内部运行机制,就会面临无法通过这些“简单”的 API 准确表达他们的需求。在这种情况下,一些开发者可能会误认为是 SwiftUI 的能力有限,而没有意识到问题可能出在自己对框架理解不够深入。
上面是我在文章《几个常见的关于 SwiftUI 的误解》中的一段文字。部分读者对此表示强烈反对,这些读者往往具备 UIKit 编程背景且使用 SwiftUI 时间不长。他们认为 SwiftUI 完全无法和 UIKit 媲美,许多在 UIKit 中轻松完成的工作在 SwiftUI 中难以实现。即便一些有其他声明式框架经验的开发者也认为,SwiftUI 在声明方式上有许多反直觉之处。
我从不否认 SwiftUI 在功能丰富度方面相较于 UIKit 仍有相当的差距。但是,这些开发者是否认真思考过:在学习和使用 SwiftUI 时,是否做好了转换思维的心理准备?是否抱着接受和拥抱新事物的态度来尝试 SwiftUI?你当前的“直觉”是否天生如此,还是在其他经验的基础上形成?
经验对一个人的成长很重要,但恪守经验,被其束缚反倒会影响我们走得更远。经验是柄双刃剑,让其为我所用,不要被其反伤。
如果您发现这份周报或我的博客对您有所帮助,可以考虑通过 爱发电,Buy Me a Coffee 支持我的创作。
原创
几个常见的关于 SwiftUI 的误解
本文旨在探讨与 SwiftUI 有关的几个常见的误解,以帮助开发者更好地理解和运用 SwiftUI。这些误区包括:对 SwiftUI 学习难度的认知、跨平台开发的期望、框架功能的范畴、以及代码量的误解等。通过澄清这些观念,希望能为 SwiftUI 开发者提供更清晰的学习方向和使用策略。
近期推荐
使用 SwiftUI Canvas 创造魔幻粒子效果 ( Magical Particle Effects with SwiftUI Canvas )
Canvas 作为 SwiftUI 中的一个强大工具,允许开发者以更接近底层的方式绘制内容,从而显著提升运行效率。Pavel Zak 在这篇文章中展示了如何巧妙运用 SwiftUI 的 Canvas 视图来创造令人惊叹的魔法粒子效果。特别值得一提的是,文中所有炫酷的视觉效果都完全依托于 SwiftUI 框架,不仅展现了出色的视觉体验,还保证了优秀的运行性能。
从磁盘加载 UIImage 时的内存消耗 ( Memory Consumption when Loading UIImage from Disk )
由于 SwiftUI 不支持直接通过路径的方式加载图像,当创建大量 Image
实例后,内存消耗可能会迅速增加。Antoine van der Lee 在这篇文章中深入探讨了加载图像时的内存管理问题。作者详细分析了几种有效的优化策略:首先考虑是否确实需要将图片存储在内存中,然后探讨了使用 UIImage(contentsOfFile:)
来规避系统缓存的方法,最后提出了如何实现一个高效的图片缓存机制来减少内存占用。通过丰富的代码示例和性能对比,Antoine 展示了如何在应用性能和内存占用之间取得良好平衡。
Swift 与 TypeScript 对比速查表 ( Swift VS TypeScript Comparison CheatSheet )
Luca Ban 拥有多年的 TypeScript 开发经验,最近开始使用 Swift 开发 VisionOS 应用。他发现尽管 TS 和 Swift 在理念上有些类似,但在目前进行的项目中,仍需要找到更加原生(Swifty)的方式来使用 Swift 语言。因此,他创建了这个快速参考,方便开发者在两种语言间找到等效方法。这个对照表详细列出了 TypeScript 和 Swift 中常见方法的对比,帮助开发者快速适应并提高效率。该网站还欢迎开发者们提交 Pull Request,进一步丰富内容。
提升 Xcode 模板中 SwiftData 的错误处理 ( Leveling Up SwiftData Error Handling in Xcode Templates )
尽管 Xcode 为 SwiftData 应用提供了默认模板,但在错误处理方面还有很大的改进空间。Mike Buss 提出了一种更健壮的方法来处理 ModelContainer
创建时可能出现的错误。相比于默认模板中使用 fatalError()
导致应用崩溃的做法,新方法通过状态管理和自定义的错误视图,为用户提供了更友好的体验。
本文介绍的方法不仅适用于 SwiftData,还可以应用于任何在应用初始化时可能产生错误的场景。
Swift 中的 async/await:完整工具箱 ( Async await in Swift: The Full Toolkit )
Jacob Bartlett 从一个常见的 iOS 面试问题出发,全面讲解了 Swift 并发工具箱中的核心组件,包括 async/await、async let、Task、Task group、Actor、MainActor、Sendable、Continuations、AsyncSequence、AsyncStream 以及 Async Algorithms。对于每种工具,Jacob 不仅解释了其基本概念和语法,还提供了实际应用场景和代码示例,帮助读者理解何时何地使用这些工具最为合适。
在 Xcode 16 中使用 @DebugDescription ( Using @DebugDescription in Xcode 16 )
在 Swift 中,CustomDebugStringConvertible
协议让开发者可以自定义数据在调试状态下的表述方式以优化体验。在本文中,Aryaman Sharda 介绍了 Xcode 16 新增的 @DebugDescription
宏,该宏在 CustomDebugStringConvertible
的基础上更进了一步,允许开发者直接在 Xcode 的变量检查器中看到自定义的调试描述,而无需使用 print
或 po
命令。作者指出,清晰、信息丰富的调试输出对于理解代码行为至关重要。
> "Extracting the essence from experience without being bound by it is both important and difficult."
I found this bit really profound, also explains why mentoring well is such a rare skill