Weekly Comment
Making Swift Stronger, Keeping It Simple
2024 marks the sixth year since I received a kidney transplant. Reflecting on this year, my personal health has been stable and smooth. As the year drew to a close, I received two exciting pieces of news—two groundbreaking advancements in xenotransplantation of kidneys, which fill me with hope for the future:
The latest research results announced by Professor Chen Gang's team in China show that a monkey that received a gene-edited pig kidney has successfully survived for six months. For organ transplant patients, the first six months post-surgery are the most critical and life-threatening period. Successfully passing this period significantly increases the likelihood of long-term survival.
53-year-old Towana Looney underwent a gene-edited pig kidney transplant at NYU Langone Health, after which her health stabilized, and she was successfully discharged on December 6th (11 days post-surgery).
Kidney transplantation is the most ideal treatment for patients with uremia. However, the shortage of donor organs means that many patients cannot receive transplant surgery in a timely manner. To address this challenge, the scientific community has been exploring multiple avenues, with xenotransplantation undoubtedly being the most promising path.
With the rapid advancement of gene-editing technology, the progress in xenotransplantation has accelerated. In recent years, this field has made continuous progress. However, despite the relentless efforts of researchers, until today, there have been no successful long-term survival cases of live transplants.
Towana Looney's case is undoubtedly a milestone breakthrough. She was not only a kidney donor in the past but has now become a beneficiary of xenotransplantation, making her the best witness to this technological breakthrough. Her resilience and courage bring hope to millions of patients waiting for organs. As a transplant patient myself, I sincerely wish her a speedy recovery and a return to a warm family life.
At the same time, I am deeply grateful to all the researchers, doctors, and team members involved in the research and treatment of xenotransplantation. It is their selfless dedication and continuous innovation that have allowed us to see the light in the face of such a significant challenge. I firmly believe that as xenotransplantation technology continues to mature, more patients like Towana will be reborn through this technology in the future.
Previous Issue|Newsletter Archive
This Week’s Sponsor
The Midst: Share Locally, Connect Globally
The Midst redefines instant messaging with a geo-focused twist! Posts are tied to your location, visible only to those exploring your area. As both an info hub and a megaphone for your ideas, The Midst is perfect for sharing breaking news or spontaneous thoughts. With posts disappearing after 24 hours, the platform remains fresh and dynamic.
Originals
Traps and Countermeasures for Abnormal onAppear Calls in SwiftUI
onAppear
is an extremely crucial lifecycle method in SwiftUI, used to inject key logic when a view is presented. Since view instances may be created and rebuilt frequently, developers often choose to prepare data and perform initialization operations within these methods. In theory, the timing of these lifecycle method calls should be predictable and consistent. However, in certain specific scenarios, onAppear
may be called unexpectedly and unnecessarily. This not only can lead to performance overhead but also may cause uncontrollable changes in the application’s state. This article will uncover this easily overlooked SwiftUI behavior trap and provide temporary countermeasures.
Recent Selections
The Ultimate Guide to Validation Patterns in SwiftUI
Validation ensures that applications handle accurate and meaningful data by providing clear feedback to help users correct invalid input and enhance the user experience. Mohammad Azam showcases several validation patterns available in SwiftUI, ranging from simple form validation to advanced real-time feedback and reusable component implementations. Azam also draws inspiration from Flutter’s live validation and ASP.NET’s model validation methods, highlighting the importance of cross-framework learning. This article serves as a powerful guide for developers to build efficient validation mechanisms.
SwiftUI Data Flow: Passing Data Between Views
Data flow is a key topic of discussion in SwiftUI. Matteo Manferdini provides a detailed overview of common SwiftUI data transfer mechanisms, including @State
, @Binding
, and @Environment
, while exploring how these mechanisms integrate with app architectures like MVC and MVVM to achieve clear and flexible data management. A standout feature of this article is its use of intuitive flowcharts to help readers understand the movement of data between views and models.
Xcode Library Customization with SPM Plugin
By implementing the LibraryContentProvider
protocol, developers can add custom components, such as SwiftUI views and modifiers, to the Xcode Library for quick reuse within projects. However, this approach requires manual updates to the code, which can be time-consuming. Artem Novichkov proposes an automated solution using the Swift Package Manager (SPM) plugin. By parsing Swift source code, the plugin automatically extracts structs conforming to the View
protocol and generates the corresponding LibraryContent.swift
file, dynamically integrating these components into the Xcode Library. This method showcases the power of SPM plugins and SwiftSyntax, providing developers with a more flexible toolchain.
Noncopyable Types in Swift
In Swift, types are copyable by default. However, in certain scenarios, copyability can lead to issues. To address this, Swift 5.9 introduced noncopyable types, offering developers stricter ownership control. Natalia Panferova provides an in-depth explanation of the usage and considerations of noncopyable types. While these types might not be suitable for every project, they are a highly effective tool for scenarios requiring unique ownership, enhancing code safety and efficiency.
Sending vs Sendable in Swift
The sending
keyword introduced in Swift 6 provides a more flexible approach to concurrent programming. Unlike the stricter limitations of @Sendable
, sending
allows capturing non-Sendable
types but ensures that these objects cannot be accessed outside the closure, thereby avoiding potential data race issues. In this article, Donny Wals explores the differences between sending
and Sendable
, explaining why the Swift team transitioned task and closure parameters from @Sendable
to sending
. This improvement enhances Swift's support for complex concurrency scenarios, making it a standout feature in Swift 6.
Creating a SwiftUI Text View with Tappable Links
While developers can directly use Markdown text within Text
, this approach lacks flexibility. To address this, Daniel Saidi introduced the LinkText
component in SwiftUIKit. LinkText
allows developers to combine plain text and tappable links within a single view, apply unique styles to each link, and inject global styles. This method simplifies code and enhances maintainability, making it a practical solution for building interactive text views in SwiftUI.
肘子的话
异种肾脏移植取得突破
2024 年是我接受肾脏移植后的第六个年头。回望这一年,个人的健康状况平稳顺遂。就在岁末之际,我看到了两则令人振奋的消息——两项异种肾脏移植的突破性进展,让我对未来充满希望:
中国陈刚教授团队最新公布的研究成果显示:一只接受基因编辑猪肾脏的猕猴已成功存活半年之久。对器官移植患者而言,术后前六个月是最为关键的生死考验。若能平安度过,便意味着长期存活的可能性大幅提升。
53 岁的 Towana Looney 在纽约大学朗格尼医学中心接受基因编辑猪肾移植手术后,身体状况稳定,并于 12 月 6 日 (术后第 11 天)顺利出院。
肾移植是尿毒症患者最理想的治疗方案。然而,供体器官短缺的困境,让众多患者无法及时获得移植手术的机会。为应对这一挑战,科技界在多个方向上开展探索,其中异种器官移植无疑是最令人期待的一条路径。
随着基因编辑技术日新月异,异种器官移植的步伐也随之加快。近年来,这一领域取得了持续的进展。然而,尽管科研工作者不懈努力,直至今日,仍未出现长期存活的活体移植成功案例。
Towana Looney 的病例无疑是一个具有里程碑意义的突破。她不仅曾是一位肾脏捐献者,如今又成为了异种移植的受益者,堪称这项技术突破的最佳见证者。她的坚韧与勇气为千万等待器官的患者带来了希望。作为一名移植患者,我由衷祝愿她早日康复,重返温暖的家庭生活。
与此同时,我也深深感激所有投身异种器官移植研究与治疗的科研人员、医生和团队成员。正是因为他们的无私奉献与持续创新,才让我们在面对如此重大挑战时看到了曙光。我坚信,随着异种器官移植技术的日渐成熟,未来将有更多像 Towana 这样的患者,因这一技术而获得重生。
本期赞助
The Midst: 本地分享,全球连接
The Midst 是一个与众不同的即时信息网络,专注地理位置。你的帖子自动绑定当前位置,只有当其他用户将地图移动到你所在区域时,才能发现你的内容。移动地图,轻松了解各地热议话题。The Midst 是信息的前沿,也是心情的窗口。无论是周围新鲜事,还是灵光一现的想法,尽在 The Midst。帖子 24 小时后自动消失,让信息始终新鲜有活力。
原创
SwiftUI onAppear 异常调用的陷阱与应对策略
onAppear
是 SwiftUI 中极其关键的生命周期方法,用于在视图呈现时注入关键逻辑。由于视图实例可能会频繁地被创建和重建,开发者通常会选择在这些方法中准备数据、执行初始化操作。理论上,这些生命周期方法的调用时机应当是可预测和一致的。然而,在某些特定场景下,onAppear
可能会出现非预期的、不必要的调用,这不仅可能导致性能开销,更可能引发应用状态的不可控变化。本文将揭示这一容易被忽视的 SwiftUI 行为陷阱,并提供临时的应对策略。
近期推荐
SwiftUI 验证模式终极指南 (The Ultimate Guide to Validation Patterns in SwiftUI)
验证确保应用处理的是准确且有意义的数据,通过提供清晰的反馈,帮助用户修正错误输入,提升用户体验。Mohammad Azam 展示了在 SwiftUI 中可以使用的一些验证模式,从简单的表单验证到复杂的实时反馈和复用组件的实现,提供了多种技术方案。同时,Azam 借鉴了 Flutter 的实时校验和 ASP.NET 的模型验证方法,展示了跨框架技术学习的重要性,为开发者提供了构建高效验证机制的有力工具。
SwiftUI 数据流:视图间的数据传递 (SwiftUI Data Flow: Passing Data Between Views)
对于 SwiftUI 来说,数据流是一个值得深入探讨的话题。Matteo Manferdini 详细介绍了 SwiftUI 中常用的数据传递机制,包括 @State
、@Binding
、@Environment
等,并探讨了如何结合应用架构(如 MVC 和 MVVM)实现清晰且灵活的数据管理。本文的亮点是使用了多个直观的流程图,帮助读者深入理解数据在视图与模型间的流转逻辑。
使用 SPM 插件自定义 Xcode Library (Xcode Library Customization with SPM Plugin)
通过实现 LibraryContentProvider
协议,开发者可以将自定义组件(如 SwiftUI 视图和修饰符)添加到 Xcode Library 中,便于在项目中快速复用组件。然而,这种方式需要开发者手动更新代码,维护成本较高。Artem Novichkov 提出了利用 Swift Package Manager (SPM) 插件的自动化解决方案。通过解析 Swift 源代码,自动提取符合 View
协议的结构体,生成对应的 LibraryContent.swift
文件,并将这些组件动态加入 Xcode Library 中,从而简化了维护流程,提高了工作效率。这种方法充分展示了 SPM 插件与 SwiftSyntax 的强大能力,为开发者提供了更灵活的工具链支持。
Swift 中的不可复制类型 (Noncopyable Types in Swift)
在 Swift 中,类型默认是可复制的。然而,在某些场景中,可复制性可能导致问题。为了解决这些问题,Swift 5.9 引入了不可复制类型(Noncopyable Types),为开发者提供了更严格的所有权控制。Natalia Panferova 在文中详细阐述了不可复制类型的用法和注意事项。虽然不可复制类型可能不适用于所有项目,但在需要确保唯一所有权的场景中,它是一项极为强大的工具,为开发者提供了更高的代码安全性和效率。
Swift 中的 Sending 与 Sendable 比较 (Sending vs Sendable in Swift)
Swift 6 中新增的 sending
关键字为并发编程提供了更灵活的工具。与 @Sendable
的严格限制不同,sending
允许捕获非 Sendable
类型,但要求这些对象在闭包之外不可再被访问,从而有效避免潜在的数据竞争问题。在文章中,Donny Wals 深入分析了 sending
与 Sendable
的异同,并解释了 Swift 团队为何将任务和闭包的参数从 @Sendable
更改为 sending
。这一改进增强了 Swift 对复杂并发场景的支持,是 Swift 6 的一大亮点。
创建带可点击链接的 SwiftUI 文本视图 (Creating a SwiftUI Text View with Tappable Links)
虽然开发者可以直接在 Text
中使用 Markdown 文本,但这种方式缺乏一定的灵活性。为了解决这一问题,Daniel Saidi 为 SwiftUIKit 添加了 LinkText
组件。LinkText
允许开发者在一个视图中灵活组合普通文本和可点击链接,并为每个链接应用独立样式,同时支持全局样式的注入。这种方式既简化了代码,又提高了可维护性。
家里也有很亲近的人有相似的问题。希望这项技术能尽快用到临床上吧。希望肘子老师可以保重身体,健健康康!一定要作息规律,保持好心态!