Weekly Comment
Since its official debut in 2014, Swift has entered its tenth year of development. Although Swift began supporting Linux at the end of 2015, its promotion and application on non-Apple platforms have been slow over the years, and many still regard Swift as a language exclusive to the Apple ecosystem.
However, it's heartening that the Swift community has never given up on the dream of bringing Swift to a broader range of platforms. Thanks to the continuous enrichment of language features and the ongoing optimization of the toolchain, Swift has made significant progress in cross-platform development in the last two to three years.
Firstly, the Swift Server Workgroup (SSWG) developed Swift for Visual Studio Code, greatly enhancing the Swift development experience on VSCode, providing developers with a more convenient cross-platform development environment. Moreover, with the introduction of new language features and third-party libraries that utilize these features, Swift's performance on the server side has also been further strengthened. With the Swift-native Foundation framework, even developers accustomed to the Apple development ecosystem can now use an increasing number of familiar APIs on other platforms.
Besides the community's efforts, the success of a language on other platforms also depends on some luck and the appearance of iconic products. With the upcoming release of the Windows version of the Arc browser this year, more Windows users and developers are expected to encounter and learn about Swift through this large desktop application primarily developed in Swift, undoubtedly having a positive impact on the popularization of Swift on other platforms.
A few days ago, OpenSwiftUI released version 0.10. Since the birth of SwiftUI, developers in the community have continuously attempted to port it to other platforms. While the implementation of declarative UI parts was relatively smooth, reactive processing has always been a challenge. In this new version, for the first time, the core foundation of the SwiftUI framework - AttributeGraph - was replicated on non-Apple platforms, allowing the full advantages of SwiftUI to be realized on other platforms.
In the past two years, I have also learned about two unreleased SwiftUI clone frameworks, one of which is also aimed at all platforms.
Swift has not forgotten its exploration in the embedded field. The community has not only added features and examples more suitable for embedded development but also early manufacturers have launched easy-to-use integrated development kits.
With Swift's continuous evolution and the joint efforts of the community and all parties, Swift is bound to be seen on more platforms and in more application scenarios in the coming years, allowing Swift developers to apply their skills in a broader field.
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
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.
Recent Selections
Exploring ABI stability, @frozen, and library evolution mode
Since Swift 5, Swift has achieved ABI stability, a breakthrough feature that ensures applications and libraries compiled with different versions of Swift can interact seamlessly without the need for recompilation. Furthermore, the library evolution mode grants Swift libraries the ability to update over time without compromising compatibility with older versions. Aryaman Sharda delves deep into ABI stability and library evolution mode in this article, providing detailed examples of the application of the @frozen
keyword in enumerations and structs. The combined effect of these features strengthens the stability and backward compatibility of the Swift ecosystem, offering developers a smoother and more efficient development experience.
Exploring Claude: Streaming Messages Using Claude 3 Haiku
Since last year, generative artificial intelligence technology has experienced explosive growth. Beyond its application in chat tools and assistant scenarios, in which other areas can generative AI be utilized? In this article, Rudrank Riyam reveals how to use Claude 3 Haiku—an economical and efficient backend service—to provide streaming message functionality for music applications. When a user queries a certain music genre, the application sends this query as a message to Claude 3 Haiku, which then returns recommended data in a streaming format.
For me, the application of AI has always been limited to assistant tools. However, during the recent Let's VisionOS 2024 event, Jane's presentation showed us how to utilize generative AI services to process natural language and return JSON data in a predetermined format. This gave me many new insights. Such methods and ways of thinking are expected to be quickly adopted by a broader range of developers, bringing more convenient experiences to users.
Adaptive Text Color in SwiftUI based on Background
During the development process, developers often encounter an issue where text of the same color may be difficult to discern on different background colors, especially when there is insufficient contrast between the text color and the background. To address this issue, Pedro Rojas provides a solution in this article, which is to dynamically adjust the text color based on the brightness of the background color, thereby ensuring the best contrast between the text and the background and significantly improving the readability of the user interface. The article not only elaborates on the implementation process of this concept but also includes a complete demonstration showing how to apply this technique in a SwiftUI environment.
Image Processing Using Swift
Xcode Playgrounds is a powerful tool that is equally useful for beginners and experienced developers, helping users to quickly validate concepts and learn new APIs. In this article, Hina Khan demonstrates how to use Playgrounds for image processing, creating dynamic and appealing visual effects. Additionally, the built-in Quick Look feature in Playgrounds provides developers with instant feedback, greatly enhancing the efficiency of developing filters. This article introduces the basic concepts of image processing in a clear and easy-to-understand manner, enabling readers to grasp and apply these techniques to create personalized image effects.
For more tips on using Xcode Playgrounds, please refer to Exploring Xcode Playground.
In Search of a Smooth Scroll
SwiftUI offers numerous lazy containers, with those prefixed with Lazy
being primarily based on SwiftUI's native implementation. Although they enhance control capabilities, they exhibit clear deficiencies in performance. In practical development, Edvinas Byla realized that the performance of LazyVGrid
was not ideal and did not meet his specific needs for context menu behavior, leading him to opt for NSTableView
as an alternative. In transitioning to AppKit, he explored some lesser-known AppKit components, such as NSTableHeaderCell
, and built a bridge between SwiftUI and AppKit using NSViewRepresentable
. Through meticulous customization of NSTableView
and NSTableCellView
, he not only significantly optimized the scrolling experience but also addressed issues related to the context menu. In the article, the author emphasizes that his goal was not to compare the performance merits of SwiftUI versus AppKit, or to question the applicability of SwiftUI on macOS. In fact, the application's complex template editor was almost entirely developed using SwiftUI and performed impressively. This highlights the strong potential and flexibility of SwiftUI in practical applications.
肘子的话
自 2014 年正式亮相以来,Swift 已步入其发展的第十个年头。虽然自 2015 年末起 Swift 便开始支持 Linux,但长期以来,其在非苹果平台上的推广和应用进展缓慢,许多人仍旧将 Swift 视作苹果生态下的专属语言。
然而,值得高兴的是,Swift 社区从未放弃将 Swift 推向更广阔平台的梦想。得益于语言特性的持续丰富与工具链的不断优化,近两三年来,Swift 在跨平台方面取得了显著进步。
首先,Swift Server Workgroup(SSWG)所开发的 Swift for Visual Studio Code 极大地提升了在 VSCode 上开发 Swift 的体验,为开发者提供了更加方便的跨平台开发环境。此外,通过引入新的语言特性及支持这些特性的第三方库,Swift 在服务器端的表现也得到了进一步加强。随着 Foundation 框架的 Swift 原生化,即便是习惯于苹果开发生态的开发者,在其他平台上也能使用越来越多熟悉的 API。
除了社区的努力外,语言在其他平台的成功同样依赖于一些幸运和标志性产品的出现。随着 Arc 浏览器 Windows 版本在今年的发布,预计将有更多 Windows 用户和开发者通过这款主要基于 Swift 开发的大型桌面应用接触并了解到 Swift,这无疑将会对 Swift 在其他平台的普及产生积极影响。
几天前,OpenSwiftUI 发布了 0.10 版本。自 SwiftUI 问世以来,社区中不断有开发者尝试将其移植到其他平台,尽管声明式 UI 部分的实现相对顺利,但响应式处理一直是难题。在这个新版本中,首次在非苹果平台复刻了 SwiftUI 框架的核心基础——AttributeGraph,从而具备了完整地将 SwiftUI 的优势全面带到其他平台的可能性。
我在过去两年中还了解到两款尚未公开的 SwiftUI 复刻框架,其中一款同样面向全平台。
Swift 也未忘记在嵌入式领域的探索。社区不仅为语言增添了更适合嵌入式开发的 功能和示例,更有厂商推出了易于使用的 一体化开发套件。
随着 Swift 自身的持续进化以及社区与各方的共同努力,未来几年中,Swift 必将在更多平台和应用场景中展现力量,Swift 开发者的技能也将得到更广泛的应用。
如果您发现这份周报或我的博客对您有所帮助,可以考虑通过 爱发电,Buy Me a Coffee 支持我的创作。
原创
新框架、新思维:解析 Observation 和 SwiftData 框架
这是我在 Let’s VisionOS 2024 上的演讲内容。为了便于阅读,我对原始内容进行了简化,并调整为更加书面化的表达。本次分享的核心是传达这样一个中心思想:尽管这些新框架是为了解决现有框架中的问题而设计的,但我们不应被过往的经验和惯例所限制。需要用开放的心态和全新的视角去学习和使用这些新工具。将采用新框架的过程视为将项目向更安全、更现代化方向重构的绝佳机会。
近期推荐
Exploring ABI stability, @frozen, and library evolution mode
自 Swift 5 起,Swift 实现了 ABI 稳定性,这一突破性特性确保了不同版本的 Swift 编译的应用程序与库之间能够无缝交互,免除了重新编译的需求。同时,库进化模式赋予了 Swift 库以时间的推移进行更新而不损害与旧版的兼容性的能力。Aryaman Sharda 在本文中深入探讨了 ABI 稳定性与库进化模式,并通过实例详细说明了 @frozen
关键字在枚举和结构体中的应用。这些功能的共同作用强化了 Swift 生态系统的稳定性和向后兼容性,为开发者提供了更加流畅和高效的开发体验。
Exploring Claude: Streaming Messages Using Claude 3 Haiku
自去年以来,生成式人工智能技术经历了爆炸式增长。除了在聊天工具和助手应用场景外,生成式 AI 还能在哪些领域发挥作用?在这篇文章里,Rudrank Riyam 为我们揭示了如何利用 Claude 3 Haiku —— 一种既经济又高效的后端服务,来为音乐应用提供消息的流式处理功能。当用户查询某个音乐风格时,应用便会把这个查询作为消息发送给 Claude 3 Haiku,后者随后以流式的形式返回推荐数据。
对我而言,对 AI 的应用一直局限于辅助工具。然而,在最近的 Let's VisionOS 2024 活动中,Jane 的演讲向我们展示了如何运用生成式 AI 服务处理自然语言,并以约定的格式返回 JSON 数据的技巧。这给了我许多全新的灵感。这样的方法和思维模式预计将很快被更广泛的开发者采纳,为用户带来更便捷的体验。
Adaptive Text Color in SwiftUI based on Background
在开发过程中,开发者时常遭遇这样一个问题:相同颜色的文字在不同背景色上可能难以辨认,尤其是当背景色与文字颜色对比度不足时。针对此问题,Pedro Rojas 在本篇文章中提供了一个解决方案,即依据背景色的亮度动态调整文本颜色,以此确保文本与背景之间的最佳对比度,显著提升用户界面的可读性。文章中不仅阐述了这一概念的实现过程,还附带了一个完整的演示,展现了如何在 SwiftUI 环境下应用这一技术。
Image Processing Using Swift
Xcode Playgrounds 是一款强大的工具,它对初学者和经验丰富的开发者都同样有用,能够帮助用户迅速验证概念和学习新的 API。在这篇文章中,Hina Khan 展示了如何使用 Playgrounds 进行图像处理,创造出既动态又吸引人的视觉效果。此外,Playgrounds 内置的 Quick Look 功能为开发者提供即时反馈,极大地提升了开发滤镜的效率。本文通过浅显易懂的方式介绍了图像处理的基本概念,使读者能够掌握并应用这些技术创造个性化的图像效果。
想要了解 Xcode Playgrounds 的更多使用技巧,请参考 玩转 Xcode Playground。
In Search of a Smooth Scroll
SwiftUI 提供了众多的惰性容器,以 Lazy
为前缀的容器大多基于 SwiftUI 原生实现,虽然它们增强了控制能力,但在性能方面却存在明显不足。在实际开发中,Edvinas Byla 意识到 LazyVGrid
的性能并不理想,且不能满足他对上下文菜单行为的特定需求,因此他决定采用 NSTableView
作为替代方案。在转向 AppKit 的过程中,他探讨了一些鲜为人知的 AppKit 组件,如 NSTableHeaderCell
等,并通过 NSViewRepresentable
构建了 SwiftUI 与 AppKit 之间的桥梁。通过对 NSTableView
和 NSTableCellView
的精心定制,他不仅显著优化了滚动体验,还解决了上下文菜单相关的问题。文章中,作者强调他的目的并非是要比较 SwiftUI 与 AppKit 的性能优劣,或者质疑 SwiftUI 在 macOS 上的应用适用性。实际上,该应用的复杂模板编辑器几乎完全使用 SwiftUI 开发,且表现出色。这一点彰显了 SwiftUI 在实际应用中的强大潜力和灵活性。
活动
WWDC 2024 愿望清单征集
上周,我发起的征集 WWDC 2024 愿望清单的推文收到了超过 80 条回复,其中有不少的内容与人工智能有关,例如:
为 Xcode 引入类似 Copilot 的编程助手功能
在选择国内 AI 服务合作伙伴时需要更加谨慎
提供生成式 AI 的框架或 API
此项活动仍在持续进行中。诚邀你在 这条推文 下留言分享你对 WWDC 2024 的期望。我将在 5 月 20 日从所有参与者中随机抽取出十位幸运者,每人赠送一箱🍒作为礼物。