<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>所有文章 - 宋明的博客</title><link>https://apolla.cc/posts/</link><description>宋明的博客</description><generator>Hugo 0.170.0 &amp; FixIt v1.0.0-rc.5-mwimukpl</generator><language>zh-cn</language><lastBuildDate>Tue, 17 Nov 2026 11:49:34 +0800</lastBuildDate><atom:link href="https://apolla.cc/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>基础语法</title><link>https://apolla.cc/posts/%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/</link><pubDate>Mon, 08 Apr 2024 15:42:24 +0800</pubDate><guid>https://apolla.cc/posts/%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;blockquote&gt;&#10;&lt;p&gt;用var定义变量，let定义常量，编译器能自动推断出变量\常量的类型&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 class="heading-element" id="常量"&gt;&lt;span&gt;常量&lt;/span&gt;&#10; &lt;a href="#%e5%b8%b8%e9%87%8f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ol&gt;&#10;&lt;li&gt;只能赋值1次&lt;/li&gt;&#10;&lt;li&gt;它的值不要求在编译时期确定，但使用之前必须赋值1次&#10;!&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 class="heading-element" id="常见数据类型"&gt;&lt;span&gt;常见数据类型&lt;/span&gt;&#10; &lt;a href="#%e5%b8%b8%e8%a7%81%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;!&lt;/p&gt;</description></item><item><title>1.5 SwiftUI 中的属性包装器</title><link>https://apolla.cc/posts/1.5-swiftui-%E4%B8%AD%E7%9A%84%E5%B1%9E%E6%80%A7%E5%8C%85%E8%A3%85%E5%99%A8/</link><pubDate>Wed, 11 Oct 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.5-swiftui-%E4%B8%AD%E7%9A%84%E5%B1%9E%E6%80%A7%E5%8C%85%E8%A3%85%E5%99%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;以下内容适用于 iOS 16/macOS 13 以上的系统，iOS 17/macOS 14 新增的 &lt;a href="https://developer.apple.com/documentation/observation" target="_blank" rel="external nofollow noopener noreferrer"&gt;Observation&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 框架简化了 SwiftUI 的状态管理，苹果的文档有非常详细的使用说明和迁移教程，笔者也写了一篇关于 Observation 的文章以供参考：&lt;a href="https://www.woter.xyz/blog/ios/swiftui/swiftui-observation-combine" target="_blank" rel="external nofollow noopener noreferrer"&gt;《有了 Observation，SwiftUI 还需要 Combine 吗》&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;。&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>1.3.3 模态（Modal）</title><link>https://apolla.cc/posts/1.3.3-%E6%A8%A1%E6%80%81modal/</link><pubDate>Tue, 10 Oct 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.3.3-%E6%A8%A1%E6%80%81modal/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;模态是除导航之外的另一种重要的信息呈现方式，它通常以一种临时的界面和用户交互。如果把导航栈内的界面比作“主线”的话，模态就是分出来的“支线”。模态视图完成它的使命后，用户最终会回归至“主线”情景。&lt;/p&gt;&#10;&lt;p&gt;关于模态的更多设计语言以及使用场景，可以参考 &lt;a href="https://developer.apple.com/design/human-interface-guidelines/patterns/modality" target="_blank" rel="external nofollow noopener noreferrer"&gt;《人机界面指南》&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;。&lt;/p&gt;</description></item><item><title>4.3 图表（Charts）</title><link>https://apolla.cc/posts/4.3-%E5%9B%BE%E8%A1%A8charts/</link><pubDate>Tue, 10 Oct 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4.3-%E5%9B%BE%E8%A1%A8charts/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="基础"&gt;&lt;span&gt;基础&lt;/span&gt;&#10; &lt;a href="#%e5%9f%ba%e7%a1%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/charts" target="_blank" rel="external nofollow noopener noreferrer"&gt;Charts&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 是 iOS 16/macOS 13 的新特性，用于构建各种精美的图表，如柱状图、散点图、曲线图等。&lt;/p&gt;</description></item><item><title>1.4 SwiftUI 视图管理机制</title><link>https://apolla.cc/posts/1.4-swiftui-%E8%A7%86%E5%9B%BE%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/</link><pubDate>Wed, 02 Aug 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.4-swiftui-%E8%A7%86%E5%9B%BE%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="视图的身份标识identity"&gt;&lt;span&gt;视图的身份标识（Identity）&lt;/span&gt;&#10; &lt;a href="#%e8%a7%86%e5%9b%be%e7%9a%84%e8%ba%ab%e4%bb%bd%e6%a0%87%e8%af%86identity" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;在 UIKit/AppKit 的世界，UIView/NSView 实例是引用类型（class），程序通过指针来引用和区分对象。要实现界面的更新，我们首先需要找到对应的视图，然后下达相应的指令，比如改变视图的位置或者颜色，然后界面才会随之更新，这是典型的命令式编程。&lt;/p&gt;</description></item><item><title>3.5 滑动视图（ScrollView）</title><link>https://apolla.cc/posts/3.5-%E6%BB%91%E5%8A%A8%E8%A7%86%E5%9B%BEscrollview/</link><pubDate>Tue, 25 Jul 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.5-%E6%BB%91%E5%8A%A8%E8%A7%86%E5%9B%BEscrollview/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="基础"&gt;&lt;span&gt;基础&lt;/span&gt;&#10; &lt;a href="#%e5%9f%ba%e7%a1%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;ScrollView 初始化方法如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;init(&#10; _ axes: Axis.Set = .vertical,&#10; showsIndicators: Bool = true,&#10; content: () -&amp;gt; Content&#10;)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;axes&lt;/code&gt; 用于指定滑动的方向，默认是垂直方向。&lt;/p&gt;</description></item><item><title>2.2 图片</title><link>https://apolla.cc/posts/2.2-%E5%9B%BE%E7%89%87/</link><pubDate>Mon, 24 Jul 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.2-%E5%9B%BE%E7%89%87/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="image"&gt;&lt;span&gt;Image&lt;/span&gt;&#10; &lt;a href="#image" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/image" target="_blank" rel="external nofollow noopener noreferrer"&gt;Image&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 用于展示图片，它可以加载资源包（Bundle）中的图片和系统内置的图标。&lt;/p&gt;</description></item><item><title>4.4 基础动画</title><link>https://apolla.cc/posts/4.4-%E5%9F%BA%E7%A1%80%E5%8A%A8%E7%94%BB/</link><pubDate>Mon, 24 Jul 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4.4-%E5%9F%BA%E7%A1%80%E5%8A%A8%E7%94%BB/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;视图是数据状态的反映，状态的更新会驱动视图的更新。如果没有动画过渡，视图的变化会显得突兀。SwiftUI 提供了几种基于状态的动画，这些都是非常常用和基础的添加动画的方式。&lt;/p&gt;&#10;&lt;h1 class="heading-element" id="animation-方法"&gt;&lt;span&gt;.animation 方法&lt;/span&gt;&#10; &lt;a href="#animation-%e6%96%b9%e6%b3%95" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;修饰器方法 &lt;a href="https://developer.apple.com/documentation/swiftui/view/animation(_:value:)" target="_blank" rel="external nofollow noopener noreferrer"&gt;animation(_:value:)&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 可以方便的创建动画，它的声明如下：&lt;/p&gt;</description></item><item><title>4.5 转场动画</title><link>https://apolla.cc/posts/4.5-%E8%BD%AC%E5%9C%BA%E5%8A%A8%E7%94%BB/</link><pubDate>Mon, 24 Jul 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4.5-%E8%BD%AC%E5%9C%BA%E5%8A%A8%E7%94%BB/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="内置转场动画"&gt;&lt;span&gt;内置转场动画&lt;/span&gt;&#10; &lt;a href="#%e5%86%85%e7%bd%ae%e8%bd%ac%e5%9c%ba%e5%8a%a8%e7%94%bb" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;转场动画用于视图的添加、移除，使用如下方法为视图添加转场：&lt;/p&gt;</description></item><item><title>3.6 列表（List）</title><link>https://apolla.cc/posts/3.6-%E5%88%97%E8%A1%A8list/</link><pubDate>Mon, 05 Jun 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.6-%E5%88%97%E8%A1%A8list/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="list"&gt;&lt;span&gt;List&lt;/span&gt;&#10; &lt;a href="#list" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="构建"&gt;&lt;span&gt;构建&lt;/span&gt;&#10; &lt;a href="#%e6%9e%84%e5%bb%ba" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;h3 class="heading-element" id="基础"&gt;&lt;span&gt;基础&lt;/span&gt;&#10; &lt;a href="#%e5%9f%ba%e7%a1%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/list" target="_blank" rel="external nofollow noopener noreferrer"&gt;List&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 提供了非常多的构建方法，各方法的入参有细微的差别，支持的平台也不尽相同，所以使用时需要小心一些，遇到疑问时及时查看文档。&lt;/p&gt;</description></item><item><title>2.3 视图样式与行为</title><link>https://apolla.cc/posts/2.3-%E8%A7%86%E5%9B%BE%E6%A0%B7%E5%BC%8F%E4%B8%8E%E8%A1%8C%E4%B8%BA/</link><pubDate>Sat, 06 May 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.3-%E8%A7%86%E5%9B%BE%E6%A0%B7%E5%BC%8F%E4%B8%8E%E8%A1%8C%E4%B8%BA/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="字体"&gt;&lt;span&gt;字体&lt;/span&gt;&#10; &lt;a href="#%e5%ad%97%e4%bd%93" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;strong&gt;SwiftUI&lt;/strong&gt; 内置了许多不同大小的字体，它们具有动态缩放特性：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;在不同设备上动态缩放。&lt;/li&gt;&#10;&lt;li&gt;在支持动态缩放的 App 中，根据手机设置的字体大小动态缩放。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;span style="background: #007BFF; color: white; padding: 3px 10px; border-radius: 5px"&gt;&amp;gt; 查看示例&lt;/span&gt;&lt;/p&gt;</description></item><item><title>2.8 使用 UIKit、AppKit</title><link>https://apolla.cc/posts/2.8-%E4%BD%BF%E7%94%A8-uikitappkit/</link><pubDate>Thu, 23 Feb 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.8-%E4%BD%BF%E7%94%A8-uikitappkit/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;SwiftUI 目前仍在发展中，相比古老而健壮的 UIKit/AppKit，它相对年轻，在生产实践中，有时我们需要借助 UIKit/AppKit 来实现 SwiftUI 难以实现的功能。&lt;/p&gt;&#10;&lt;p&gt;以下仅以 UIKit 为例进行说明，AppKit 与此类似。&lt;/p&gt;&#10;&lt;h1 class="heading-element" id="使用-uikit"&gt;&lt;span&gt;使用 UIKit&lt;/span&gt;&#10; &lt;a href="#%e4%bd%bf%e7%94%a8-uikit" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="uiviewrepresentable"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/uiviewrepresentable" target="_blank" rel="external nofollow noopener noreferrer"&gt;UIViewRepresentable&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#uiviewrepresentable" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;SwiftUI 通过 UIViewRepresentable 协议可以调用 UIKit 中的视图，比如想使用 UISearchBar：&lt;/p&gt;</description></item><item><title>5.3 自定义手势</title><link>https://apolla.cc/posts/5.3-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%89%8B%E5%8A%BF/</link><pubDate>Sun, 29 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/5.3-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%89%8B%E5%8A%BF/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;&lt;span style="background: #007BFF; color: white; padding: 3px 10px; border-radius: 5px"&gt;&amp;gt; 查看示例&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;SwiftUI 内置了一些常用手势，它们都实现了 &lt;a href="https://developer.apple.com/documentation/swiftui/gesture" target="_blank" rel="external nofollow noopener noreferrer"&gt;Gesture&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 协议。&lt;/p&gt;&#10;&lt;p&gt;如果需要自定义手势，也需要实现该协议，其声明如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;public protocol Gesture {&#10;&#10; /// The type representing the gesture&amp;#39;s value.&#10; associatedtype Value&#10;&#10; /// The type of gesture representing the body of `Self`.&#10; associatedtype Body : Gesture&#10;&#10; /// The content and behavior of the gesture.&#10; var body: Self.Body { get }&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;和 View 协议类似，body 属性是必须实现的，它的类型由关联类型 Body 定义。另一个关联类型 Value 用于保存手势的状态，在 &lt;code&gt;.onChange&lt;/code&gt; 、 &lt;code&gt;.udpating&lt;/code&gt; 等监听方法中返回该状态。&lt;/p&gt;</description></item><item><title>5.1 常见手势</title><link>https://apolla.cc/posts/5.1-%E5%B8%B8%E8%A7%81%E6%89%8B%E5%8A%BF/</link><pubDate>Sat, 28 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/5.1-%E5%B8%B8%E8%A7%81%E6%89%8B%E5%8A%BF/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h2 class="heading-element" id="tapgesturespatialtapgesture"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/tapgesture" target="_blank" rel="external nofollow noopener noreferrer"&gt;TapGesture&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;、&lt;a href="https://developer.apple.com/documentation/swiftui/spatialtapgesture" target="_blank" rel="external nofollow noopener noreferrer"&gt;SpatialTapGesture&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#tapgesturespatialtapgesture" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;要为视图添加点击手势，可以使用 &lt;a href="https://developer.apple.com/documentation/swiftui/view/gesture(_:including:)" target="_blank" rel="external nofollow noopener noreferrer"&gt;gesture(_:including:)&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 绑定一个 TapGesture 实例至视图，也可以直接在视图上使用 &lt;code&gt;.onTapGesture&lt;/code&gt; 方法。&lt;/p&gt;</description></item><item><title>5.2 手势的管理</title><link>https://apolla.cc/posts/5.2-%E6%89%8B%E5%8A%BF%E7%9A%84%E7%AE%A1%E7%90%86/</link><pubDate>Sat, 28 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/5.2-%E6%89%8B%E5%8A%BF%E7%9A%84%E7%AE%A1%E7%90%86/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="手势的状态监听"&gt;&lt;span&gt;手势的状态监听&lt;/span&gt;&#10; &lt;a href="#%e6%89%8b%e5%8a%bf%e7%9a%84%e7%8a%b6%e6%80%81%e7%9b%91%e5%90%ac" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;《常见手势》 演示了通过 &lt;code&gt;.onChanged&lt;/code&gt; 和 &lt;code&gt;.onEnded&lt;/code&gt; 分别监听手势的变化和结束。&lt;/p&gt;</description></item><item><title>4.7 动画的进阶使用</title><link>https://apolla.cc/posts/4.7-%E5%8A%A8%E7%94%BB%E7%9A%84%E8%BF%9B%E9%98%B6%E4%BD%BF%E7%94%A8/</link><pubDate>Tue, 17 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4.7-%E5%8A%A8%E7%94%BB%E7%9A%84%E8%BF%9B%E9%98%B6%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;h1 class="heading-element" id="transaction"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/transaction" target="_blank" rel="external nofollow noopener noreferrer"&gt;Transaction&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#transaction" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;事务（Transaction）是当前状态更新的上下文，包含状态变化过程中的动画。&lt;/p&gt;</description></item><item><title>4.6 动画原理</title><link>https://apolla.cc/posts/4.6-%E5%8A%A8%E7%94%BB%E5%8E%9F%E7%90%86/</link><pubDate>Fri, 13 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4.6-%E5%8A%A8%E7%94%BB%E5%8E%9F%E7%90%86/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;h1 class="heading-element" id="animatable-协议"&gt;&lt;span&gt;Animatable 协议&lt;/span&gt;&#10; &lt;a href="#animatable-%e5%8d%8f%e8%ae%ae" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;我们之所以能看见动画的效果，是因为 SwiftUI 在视图变化的首尾状态之间不断地重绘视图，一系列静态视图因“视觉暂留”作用而造成一种连续的视觉印象，让我们感觉视图动起来了，这其实也是电影的原理。比如一个视图需要从透明度 0 变成 1，SwiftUI 会在 0 和 1 之间不断地作插值运算，不断重绘不同透明度的视图（0，0.1，0.2，0.3 &amp;hellip; 1.0），直到视图不再变化。如果使用 &lt;code&gt;.linear&lt;/code&gt; 指定动画效果，那么视图的重绘过程是匀速的，如果使用 &lt;code&gt;.easeOut&lt;/code&gt;，那么重绘的过程是先快后慢，以此类推。&lt;/p&gt;</description></item><item><title>4.2 图形的绘制</title><link>https://apolla.cc/posts/4.2-%E5%9B%BE%E5%BD%A2%E7%9A%84%E7%BB%98%E5%88%B6/</link><pubDate>Mon, 09 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4.2-%E5%9B%BE%E5%BD%A2%E7%9A%84%E7%BB%98%E5%88%B6/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;h1 class="heading-element" id="canvas-的使用"&gt;&lt;span&gt;Canvas 的使用&lt;/span&gt;&#10; &lt;a href="#canvas-%e7%9a%84%e4%bd%bf%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;Canvas 字面意思就是“画布”，在这个画布上可以绘制各种图形。与 Shape 不同的是，Canvas 支持即时绘制，比如用户可以使用手指在屏幕上涂鸦。&lt;/p&gt;</description></item><item><title>3.9 自定义布局（Layout）</title><link>https://apolla.cc/posts/3.9-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%B8%83%E5%B1%80layout/</link><pubDate>Sat, 07 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.9-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%B8%83%E5%B1%80layout/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;SwiftUI 内置了丰富的布局容器，也支持简单的布局调整，它们能够解决大多数常用的布局需求。但仍然有所限制，比如 HStack 或者 VStack，它们只能在单一线性轴上进行布局，Grid 虽然可以兼顾横向和纵向，但子视图的位置不能随意调整。&lt;/p&gt;&#10;&lt;p&gt;而 Layout 协议支持自定义布局，我们可以使用它随心所欲地实现各种纷繁的布局效果。&lt;/p&gt;</description></item><item><title>4.1 形状（Shapes）的使用</title><link>https://apolla.cc/posts/4.1-%E5%BD%A2%E7%8A%B6shapes%E7%9A%84%E4%BD%BF%E7%94%A8/</link><pubDate>Sat, 07 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4.1-%E5%BD%A2%E7%8A%B6shapes%E7%9A%84%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="常用的内置形状及样式"&gt;&lt;span&gt;常用的内置形状及样式&lt;/span&gt;&#10; &lt;a href="#%e5%b8%b8%e7%94%a8%e7%9a%84%e5%86%85%e7%bd%ae%e5%bd%a2%e7%8a%b6%e5%8f%8a%e6%a0%b7%e5%bc%8f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="内置形状"&gt;&lt;span&gt;内置形状&lt;/span&gt;&#10; &lt;a href="#%e5%86%85%e7%bd%ae%e5%bd%a2%e7%8a%b6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;SwiftUI 内置了一些常用的形状，比如矩形、椭圆、正圆等。&lt;/p&gt;</description></item><item><title>3.7 表格（Table）</title><link>https://apolla.cc/posts/3.7-%E8%A1%A8%E6%A0%BCtable/</link><pubDate>Fri, 06 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.7-%E8%A1%A8%E6%A0%BCtable/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;</description></item><item><title>3.11 SwiftUI 布局总结</title><link>https://apolla.cc/posts/3.11-swiftui-%E5%B8%83%E5%B1%80%E6%80%BB%E7%BB%93/</link><pubDate>Thu, 05 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.11-swiftui-%E5%B8%83%E5%B1%80%E6%80%BB%E7%BB%93/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;pre&gt;&lt;code&gt;fileprivate struct ViewTransforming: View {&#10; @State private var aspectRatio: CGFloat = 1.0&#10; &#10; var body: some View {&#10; GroupBox(&amp;#34;aspectRatio&amp;#34;) {&#10; Rectangle()&#10; .fill(.blue.gradient)&#10; .aspectRatio(aspectRatio, contentMode: .fit)&#10; .frame(width: 80)&#10; LabeledContent(&amp;#34;aspectRatio: \(aspectRatio, specifier: &amp;#34;%.1f&amp;#34;)&amp;#34;) {&#10; Slider(value: $aspectRatio, in: 0.5...2.0)&#10; }&#10; }&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/57243677/proportional-height-or-width-in-swiftui" target="_blank" rel="external nofollow noopener noreferrer"&gt;https://stackoverflow.com/questions/57243677/proportional-height-or-width-in-swiftui&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;静态渲染适用于轻量的数据展示，比如 HStack、Grid 等，它们的内容和尺寸都相对固定。SwiftUI 会计算它们包含的所有视图，无论该视图是在屏幕内或屏幕外，如果有少量的视图需要渲染，使用它们是非常合适的。&lt;/p&gt;</description></item><item><title>3.8 共享布局信息</title><link>https://apolla.cc/posts/3.8-%E5%85%B1%E4%BA%AB%E5%B8%83%E5%B1%80%E4%BF%A1%E6%81%AF/</link><pubDate>Thu, 05 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.8-%E5%85%B1%E4%BA%AB%E5%B8%83%E5%B1%80%E4%BF%A1%E6%81%AF/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="geometryreader"&gt;&lt;span&gt;GeometryReader&lt;/span&gt;&#10; &lt;a href="#geometryreader" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/geometryreader" target="_blank" rel="external nofollow noopener noreferrer"&gt;GeometryReader&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 视图的构建闭包内返回了一个 &lt;a href="https://developer.apple.com/documentation/swiftui/geometryproxy" target="_blank" rel="external nofollow noopener noreferrer"&gt;GeometryProxy&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 类型的结构体，通过它可以获取到如下信息：&lt;/p&gt;</description></item><item><title>3.2.1 视图组</title><link>https://apolla.cc/posts/3.2.1-%E8%A7%86%E5%9B%BE%E7%BB%84/</link><pubDate>Wed, 04 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.2.1-%E8%A7%86%E5%9B%BE%E7%BB%84/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="group"&gt;&lt;span&gt;Group&lt;/span&gt;&#10; &lt;a href="#group" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;前面提到过 SwiftUI 能够将各种视图组合在一起，得益于结果生成器，关于结果生成器，参考 《Swift 中的 Result builders》 。&lt;/p&gt;</description></item><item><title>3.4 格子（Grids）</title><link>https://apolla.cc/posts/3.4-%E6%A0%BC%E5%AD%90grids/</link><pubDate>Wed, 04 Jan 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.4-%E6%A0%BC%E5%AD%90grids/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="grid"&gt;&lt;span&gt;Grid&lt;/span&gt;&#10; &lt;a href="#grid" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/grid" target="_blank" rel="external nofollow noopener noreferrer"&gt;Grid&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 和 &lt;a href="https://developer.apple.com/documentation/swiftui/gridrow" target="_blank" rel="external nofollow noopener noreferrer"&gt;GridRow&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 是 iOS 16/macOS 13 新出的功能，用于快速构建静态格子视图。&lt;/p&gt;</description></item><item><title>3.3 布局的调整</title><link>https://apolla.cc/posts/3.3-%E5%B8%83%E5%B1%80%E7%9A%84%E8%B0%83%E6%95%B4/</link><pubDate>Fri, 23 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.3-%E5%B8%83%E5%B1%80%E7%9A%84%E8%B0%83%E6%95%B4/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;调整视图布局的方法是多样的，本文主要介绍通过调整 view 的尺寸、位置以及对齐方式等来实现布局的基础调整，在实际开发中，需要选择合适的方法来实现不同的布局效果。除此以外，后面还会介绍自定义 layout 来实现更加高级和灵活地布局。&lt;/p&gt;&#10;&lt;h1 class="heading-element" id="尺寸"&gt;&lt;span&gt;尺寸&lt;/span&gt;&#10; &lt;a href="#%e5%b0%ba%e5%af%b8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="padding"&gt;&lt;span&gt;padding&lt;/span&gt;&#10; &lt;a href="#padding" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;SwiftUI 提供了多个 padding 方法，可以方便地调整视图各个方向上的内边距，从而影响布局和空间的分配。&lt;/p&gt;</description></item><item><title>3.2 堆叠（Stacks）</title><link>https://apolla.cc/posts/3.2-%E5%A0%86%E5%8F%A0stacks/</link><pubDate>Thu, 22 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.2-%E5%A0%86%E5%8F%A0stacks/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="hstackvstackzstack"&gt;&lt;span&gt;HStack、VStack、ZStack&lt;/span&gt;&#10; &lt;a href="#hstackvstackzstack" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/hstack" target="_blank" rel="external nofollow noopener noreferrer"&gt;HStack&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;、&lt;a href="https://developer.apple.com/documentation/swiftui/vstack" target="_blank" rel="external nofollow noopener noreferrer"&gt;VStack&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;、&lt;a href="https://developer.apple.com/documentation/swiftui/zstack" target="_blank" rel="external nofollow noopener noreferrer"&gt;ZStack&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 分别可以在三个线性轴 x、y、z 方向上组合视图。&lt;/p&gt;</description></item><item><title>3.1 SwiftUI 布局思想简介</title><link>https://apolla.cc/posts/3.1-swiftui-%E5%B8%83%E5%B1%80%E6%80%9D%E6%83%B3%E7%AE%80%E4%BB%8B/</link><pubDate>Wed, 21 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3.1-swiftui-%E5%B8%83%E5%B1%80%E6%80%9D%E6%83%B3%E7%AE%80%E4%BB%8B/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;SwiftUI 的布局过程大致如下：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;父视图为子视图提供布局空间。&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;若指定了尺寸，父视图提供的布局空间就是指定的尺寸范围。&lt;/li&gt;&#10;&lt;li&gt;若没指定尺寸：&#10;&lt;ul&gt;&#10;&lt;li&gt;若父视图是无边界的，比如 Color 还有下面提到的 ScrollView，它的尺寸默认是无限大（这里的无限大指可以利用的一切空间，下同）。&lt;/li&gt;&#10;&lt;li&gt;若父视图是有边界的，比如 HStack、VStack 等，它的尺寸取决于子视图的尺寸。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;子视图根据自身内容计算尺寸，并将该信息提供给父视图。&lt;/p&gt;&#10;&lt;p&gt;SwiftUI 中的视图层级类似树状结构，对每个节点视图的 body 属性的计算是个递归的过程，终止条件是遇到 body 属性为 Never 的视图，它们又称为原始视图，是这颗树上的叶子结点。这里提到的父子关系并非绝对概念，子视图可能也作为其它视图的父视图，父子其实是个相对概念。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;父视图根据子视图提供的尺寸信息计算自身尺寸并对子视图进行布局。&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;若父视图指定了尺寸，则根据指定的尺寸对子视图进行布局。&lt;/li&gt;&#10;&lt;li&gt;若父视图没指定尺寸，则根据子视图提供的尺寸信息计算自身的尺寸并对子视图进行布局。父视图在计算自身尺寸时如果它本身就是无边界的，那么它的尺寸不受子视图的影响。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;</description></item><item><title>2.6 视图生命周期方法</title><link>https://apolla.cc/posts/2.6-%E8%A7%86%E5%9B%BE%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E6%96%B9%E6%B3%95/</link><pubDate>Tue, 20 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.6-%E8%A7%86%E5%9B%BE%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E6%96%B9%E6%B3%95/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="onappear"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/view/onappear(perform:)" target="_blank" rel="external nofollow noopener noreferrer"&gt;onAppear&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#onappear" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;在视图即将出现时会触发 onAppear 方法。&lt;/p&gt;&#10;&lt;p&gt;&lt;span style="background: #007BFF; color: white; padding: 3px 10px; border-radius: 5px"&gt;&amp;gt; 查看示例&lt;/span&gt;&lt;/p&gt;</description></item><item><title>2.7 自定义视图</title><link>https://apolla.cc/posts/2.7-%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%86%E5%9B%BE/</link><pubDate>Tue, 20 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.7-%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%86%E5%9B%BE/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="自定义-view"&gt;&lt;span&gt;自定义 view&lt;/span&gt;&#10; &lt;a href="#%e8%87%aa%e5%ae%9a%e4%b9%89-view" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;SwiftUI 中的视图都是实现 View 协议的结构体，比如需要自定义一个垂直布局的 Label：&lt;/p&gt;</description></item><item><title>2.4.5 控件-样式</title><link>https://apolla.cc/posts/2.4.5-%E6%8E%A7%E4%BB%B6-%E6%A0%B7%E5%BC%8F/</link><pubDate>Mon, 19 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.4.5-%E6%8E%A7%E4%BB%B6-%E6%A0%B7%E5%BC%8F/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="色调"&gt;&lt;span&gt;色调&lt;/span&gt;&#10; &lt;a href="#%e8%89%b2%e8%b0%83" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;在 《控件-指示器》 一文提到过可以通过 &lt;code&gt;.tint&lt;/code&gt; 方法覆盖程序设置的 accentColor 来给控件指定主色调，但并非所有控件都能生效，比如使用渐变色样式时，无法应用该样式的控件会采用渐变色的第一个颜色进行渲染。&lt;/p&gt;</description></item><item><title>2.5 菜单与命令</title><link>https://apolla.cc/posts/2.5-%E8%8F%9C%E5%8D%95%E4%B8%8E%E5%91%BD%E4%BB%A4/</link><pubDate>Mon, 19 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.5-%E8%8F%9C%E5%8D%95%E4%B8%8E%E5%91%BD%E4%BB%A4/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;Menu 也是一种常用控件，用于创建菜单视图。Button、Toggle、Picker 等常用控件都可以作为 Menu 的子控件，Menu 本身还支持嵌套使用。&lt;/p&gt;&#10;&lt;p&gt;&lt;span style="background: #007BFF; color: white; padding: 3px 10px; border-radius: 5px"&gt;&amp;gt; 查看示例&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;Menu 的构建方法中有一个支持 primaryAction 的闭包，没有添加它时，单击 menu 就会弹出菜单，添加之后，单击 menu 的事件就会成为 primaryAction，长按（iOS）或者点击菜单下拉按钮（macOS）时才会弹出菜单。macOS 的下拉菜单按钮可以通过方法 &lt;a href="https://developer.apple.com/documentation/swiftui/view/menuindicator(_:)" target="_blank" rel="external nofollow noopener noreferrer"&gt;menuIndicator(_:)&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 隐藏。&lt;/p&gt;</description></item><item><title>2.4.4 控件-指示器</title><link>https://apolla.cc/posts/2.4.4-%E6%8E%A7%E4%BB%B6-%E6%8C%87%E7%A4%BA%E5%99%A8/</link><pubDate>Fri, 16 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.4.4-%E6%8E%A7%E4%BB%B6-%E6%8C%87%E7%A4%BA%E5%99%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="progressview"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/progressview" target="_blank" rel="external nofollow noopener noreferrer"&gt;ProgressView&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#progressview" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;ProgressView 有菊花和进度条两种样式，&lt;span style="background: #007BFF; color: white; padding: 3px 10px; border-radius: 5px"&gt;&amp;gt; 查看示例&lt;/span&gt; 。&lt;/p&gt;</description></item><item><title>2.4.1 控件-按钮</title><link>https://apolla.cc/posts/2.4.1-%E6%8E%A7%E4%BB%B6-%E6%8C%89%E9%92%AE/</link><pubDate>Thu, 15 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.4.1-%E6%8E%A7%E4%BB%B6-%E6%8C%89%E9%92%AE/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="按钮样式"&gt;&lt;span&gt;按钮样式&lt;/span&gt;&#10; &lt;a href="#%e6%8c%89%e9%92%ae%e6%a0%b7%e5%bc%8f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/button" target="_blank" rel="external nofollow noopener noreferrer"&gt;Button&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 可以通过字符串或 view 构建，需要注意的是，通过字符串构建 Button 时， &lt;code&gt;.frame&lt;/code&gt; 是无法扩大点击范围的。因为 frame 的作用是给父视图提供尺寸信息作布局用，我们可以在 frame 的基础上更改 button 的背景，但 button 自身的尺寸依然是由自身内容（字符串）决定的。对于简单的按钮可以直接通过字符串构建，系统也提供了内置的按钮样式。如需更丰富的样式，推荐使用 view 构建，或是自定义按钮样式。&lt;/p&gt;</description></item><item><title>2.4.2 控件-链接</title><link>https://apolla.cc/posts/2.4.2-%E6%8E%A7%E4%BB%B6-%E9%93%BE%E6%8E%A5/</link><pubDate>Thu, 15 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.4.2-%E6%8E%A7%E4%BB%B6-%E9%93%BE%E6%8E%A5/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="link"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/link" target="_blank" rel="external nofollow noopener noreferrer"&gt;Link&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#link" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;Link 可以打开网页链接，如果通用链接（Universal Link）支持，会跳转到对应的 App 页面。&lt;/p&gt;</description></item><item><title>2.4.3 控件-选择器</title><link>https://apolla.cc/posts/2.4.3-%E6%8E%A7%E4%BB%B6-%E9%80%89%E6%8B%A9%E5%99%A8/</link><pubDate>Thu, 15 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.4.3-%E6%8E%A7%E4%BB%B6-%E9%80%89%E6%8B%A9%E5%99%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="toggle"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/toggle" target="_blank" rel="external nofollow noopener noreferrer"&gt;Toggle&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#toggle" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;Toggle 是表示开关状态的控件，默认的颜色风格是绿色，使用 &lt;code&gt;.tint&lt;/code&gt; 可以改变颜色。&lt;/p&gt;</description></item><item><title>2.1.1 文本-Text</title><link>https://apolla.cc/posts/2.1.1-%E6%96%87%E6%9C%AC-text/</link><pubDate>Mon, 05 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.1.1-%E6%96%87%E6%9C%AC-text/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="字符串"&gt;&lt;span&gt;字符串&lt;/span&gt;&#10; &lt;a href="#%e5%ad%97%e7%ac%a6%e4%b8%b2" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;SwiftUI 中涉及到字符串的地方，基本都支持普通的字符串和本地化字符串。&lt;/p&gt;</description></item><item><title>2.1.2 文本-Label</title><link>https://apolla.cc/posts/2.1.2-%E6%96%87%E6%9C%AC-label/</link><pubDate>Mon, 05 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.1.2-%E6%96%87%E6%9C%AC-label/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="构建方法"&gt;&lt;span&gt;构建方法&lt;/span&gt;&#10; &lt;a href="#%e6%9e%84%e5%bb%ba%e6%96%b9%e6%b3%95" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/label" target="_blank" rel="external nofollow noopener noreferrer"&gt;Label&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 可以快速生成图片和文字的组合，默认布局是左图右文。&lt;/p&gt;</description></item><item><title>2.1.3 文本-输入</title><link>https://apolla.cc/posts/2.1.3-%E6%96%87%E6%9C%AC-%E8%BE%93%E5%85%A5/</link><pubDate>Mon, 05 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2.1.3-%E6%96%87%E6%9C%AC-%E8%BE%93%E5%85%A5/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="textfield"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/textfield" target="_blank" rel="external nofollow noopener noreferrer"&gt;TextField&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#textfield" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="构建及样式"&gt;&lt;span&gt;构建及样式&lt;/span&gt;&#10; &lt;a href="#%e6%9e%84%e5%bb%ba%e5%8f%8a%e6%a0%b7%e5%bc%8f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;TextField 具有多个构建方法，最简单的如下：&lt;/p&gt;</description></item><item><title>1.3.4 工具栏（Toolbars）</title><link>https://apolla.cc/posts/1.3.4-%E5%B7%A5%E5%85%B7%E6%A0%8Ftoolbars/</link><pubDate>Thu, 01 Dec 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.3.4-%E5%B7%A5%E5%85%B7%E6%A0%8Ftoolbars/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;SwiftUI 将 navigationBar、bottomBar、tabBar、windowToolbar 等视图的功能和样式都整合在了工具栏中，使用时调用的 api 都是以 .toolbar 开头的。&lt;/p&gt;&#10;&lt;p&gt;比如要给导航栏右侧添加一个分享按钮：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;#if os(iOS)&#10;NavigationStack {&#10; ContentView()&#10; .toolbar {&#10; ToolbarItem(placement: .navigationBarTrailing) {&#10; Button {&#10; } label: {&#10; Image(systemName: &amp;#34;square.and.arrow.up&amp;#34;)&#10; }&#10; }&#10; }&#10;}&#10;#endif&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;span style="background: #007BFF; color: white; padding: 3px 10px; border-radius: 5px"&gt;&amp;gt; 查看示例&lt;/span&gt;&lt;/p&gt;</description></item><item><title>1.3.0 TabView 的使用</title><link>https://apolla.cc/posts/1.3.0-tabview-%E7%9A%84%E4%BD%BF%E7%94%A8/</link><pubDate>Wed, 23 Nov 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.3.0-tabview-%E7%9A%84%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="基础使用"&gt;&lt;span&gt;基础使用&lt;/span&gt;&#10; &lt;a href="#%e5%9f%ba%e7%a1%80%e4%bd%bf%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/tabview" target="_blank" rel="external nofollow noopener noreferrer"&gt;TabView&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 等同于 UIKit 中的 UITabBar，先来看看它的基础使用：&lt;/p&gt;</description></item><item><title>1.3.2 导航之 NavigationSplitView</title><link>https://apolla.cc/posts/1.3.2-%E5%AF%BC%E8%88%AA%E4%B9%8B-navigationsplitview/</link><pubDate>Mon, 21 Nov 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.3.2-%E5%AF%BC%E8%88%AA%E4%B9%8B-navigationsplitview/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/navigationsplitview" target="_blank" rel="external nofollow noopener noreferrer"&gt;NavigationSplitView&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 用于快速构建多栏视图，它支持在 Apple 全平台上使用。根据设备屏幕尺寸（Size Class）的差异，NavigationSplitView 的样式有所不同。&lt;/p&gt;&#10;&lt;p&gt;Size Class 分为 regular 和 compact，NavigationSplitView 相应的表现分别是分栏和导航栈。正如示例所示，在 iPhone 14 Plus 或 iPhone 14 Pro Max 机型上，竖屏时 Size Class 是 compact，导航行为就是常见的 push 和 pop。而横屏时，Size Class 是 regular，视图会变成分栏样式。&lt;/p&gt;</description></item><item><title>1.3.1 导航之 NavigationStack</title><link>https://apolla.cc/posts/1.3.1-%E5%AF%BC%E8%88%AA%E4%B9%8B-navigationstack/</link><pubDate>Mon, 05 Sep 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.3.1-%E5%AF%BC%E8%88%AA%E4%B9%8B-navigationstack/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;iOS 16 废弃了 NavigationView，使用更加灵活的 &lt;a href="https://developer.apple.com/documentation/swiftui/navigationstack" target="_blank" rel="external nofollow noopener noreferrer"&gt;NavigationStack&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;、&lt;a href="https://developer.apple.com/documentation/swiftui/navigationsplitview" target="_blank" rel="external nofollow noopener noreferrer"&gt;NavigationSplitView&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 管理导航栈。二者在 iOS、iPadOS、macOS 上均可使用，但在不同平台表现不一。后文主要演示 NavigationStack 在 iPhone、iPad 上的使用，NavigationSplitView 在 iPad、Mac 上的使用。&lt;/p&gt;</description></item><item><title>1.3 SwiftUI 中的场景</title><link>https://apolla.cc/posts/1.3-swiftui-%E4%B8%AD%E7%9A%84%E5%9C%BA%E6%99%AF/</link><pubDate>Mon, 29 Aug 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.3-swiftui-%E4%B8%AD%E7%9A%84%E5%9C%BA%E6%99%AF/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;p&gt;&lt;em&gt;鉴于 Scene 比较特殊，难以在 Eul 中内置示例，笔者为其单独创建了一个样例工程，源码：&lt;a href="https://github.com/zzzwco/resources/tree/main/eul/SampleScene" target="_blank" rel="external nofollow noopener noreferrer"&gt;Eul/SampleScene&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;。&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;在 [[1.1.2 生命周期的演变]]中已经介绍过一些关于场景的概念了，下面逐一介绍 SwiftUI 内置的场景及其用法。&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="windowgroup"&gt;&lt;span&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/windowgroup?changes=latest_minor" target="_blank" rel="external nofollow noopener noreferrer"&gt;WindowGroup&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#windowgroup" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;这是最重要、最常用的场景，新建一个 SwiftUI 工程时，在 app 入口处就会看到如下代码：&lt;/p&gt;</description></item><item><title>1.2 生命周期的演变</title><link>https://apolla.cc/posts/1.2-%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E7%9A%84%E6%BC%94%E5%8F%98/</link><pubDate>Sat, 20 Aug 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.2-%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E7%9A%84%E6%BC%94%E5%8F%98/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;iOS 13 之前，app 的入口以及生命周期方法由 &lt;a href="https://developer.apple.com/documentation/uikit/uiapplicationdelegate" target="_blank" rel="external nofollow noopener noreferrer"&gt;UIApplicationDelegate&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 协议定义，通常我们使用 &lt;a href="https://developer.apple.com/documentation/uikit/uiwindow" target="_blank" rel="external nofollow noopener noreferrer"&gt;UIWindow&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 实例作为呈现界面和分发事件的窗口。&lt;/p&gt;&#10;&lt;p&gt;iOS 13 开始，iPad 支持多窗口模式。 臃肿的 AppDelegate 不堪重负，于是它将各个窗口的管理工作分配给 &lt;a href="https://developer.apple.com/documentation/uikit/uiscenedelegate" target="_blank" rel="external nofollow noopener noreferrer"&gt;UISceneDelegate&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 实例。&lt;/p&gt;&#10;&lt;p&gt;iOS 14 新增了 SwiftUI 专属的 &lt;a href="https://developer.apple.com/documentation/swiftui/app" target="_blank" rel="external nofollow noopener noreferrer"&gt;App&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 协议，它定义了 app 的结构和行为。创建一个 SwiftUI 项目时，Xcode 默认实现了一个 App 实例：&lt;/p&gt;</description></item><item><title>1.1 App 入口与配置</title><link>https://apolla.cc/posts/1.1-app-%E5%85%A5%E5%8F%A3%E4%B8%8E%E9%85%8D%E7%BD%AE/</link><pubDate>Mon, 15 Aug 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.1-app-%E5%85%A5%E5%8F%A3%E4%B8%8E%E9%85%8D%E7%BD%AE/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="入口"&gt;&lt;span&gt;入口&lt;/span&gt;&#10; &lt;a href="#%e5%85%a5%e5%8f%a3" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;当我们 &lt;a href="https://developer.apple.com/documentation/xcode/creating-an-xcode-project-for-an-app" target="_blank" rel="external nofollow noopener noreferrer"&gt;创建一个工程&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 后，首先看到的就是如下代码：&lt;/p&gt;</description></item><item><title>1.1 App 入口与配置_副本</title><link>https://apolla.cc/posts/1.1-app-%E5%85%A5%E5%8F%A3%E4%B8%8E%E9%85%8D%E7%BD%AE_%E5%89%AF%E6%9C%AC/</link><pubDate>Mon, 15 Aug 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1.1-app-%E5%85%A5%E5%8F%A3%E4%B8%8E%E9%85%8D%E7%BD%AE_%E5%89%AF%E6%9C%AC/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="入口"&gt;&lt;span&gt;入口&lt;/span&gt;&#10; &lt;a href="#%e5%85%a5%e5%8f%a3" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;当我们 &lt;a href="https://developer.apple.com/documentation/xcode/creating-an-xcode-project-for-an-app" target="_blank" rel="external nofollow noopener noreferrer"&gt;创建一个工程&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 后，首先看到的就是如下代码：&lt;/p&gt;</description></item><item><title>1.1 Swift 并发新体验</title><link>https://apolla.cc/posts/1.1-swift-%E5%B9%B6%E5%8F%91%E6%96%B0%E4%BD%93%E9%AA%8C/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/1.1-swift-%E5%B9%B6%E5%8F%91%E6%96%B0%E4%BD%93%E9%AA%8C/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="引言"&gt;&lt;span&gt;引言&lt;/span&gt;&#10; &lt;a href="#%e5%bc%95%e8%a8%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;对于诞生于 2014 年的 Swift 而言，它已不再年轻。至今我还记得初次体验 Swift 时的喜悦之情，比起冗长的 OC 而言，它更加现代、简洁、优雅。但 Swift 的前期发展是野蛮而动荡的，每次发布新版本时都会导致旧项目出现大量的报错和告警，项目迁移工作令开发者苦不堪言。不得不说，Swift 诞生之初就敢于在项目中实践并运用的人，是真的猛士。我是从 Swift 4 才开始将项目逐渐从 OC 向 Swift 迁移的，到 2019 年 Swift 5 实现了 ABI 稳定时，才全面迁移至纯 Swift 开发。&lt;/p&gt;</description></item><item><title>1.2 async、await 的使用</title><link>https://apolla.cc/posts/1.2-asyncawait-%E7%9A%84%E4%BD%BF%E7%94%A8/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/1.2-asyncawait-%E7%9A%84%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="asyncawait"&gt;&lt;span&gt;async/await&lt;/span&gt;&#10; &lt;a href="#asyncawait" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;上一节我们已经简单介绍了 async/await 的使用，接下来我们通过一个例子来加深对 async/await 的理解。如下是一个下载图片并展示在界面上的示例代码：&lt;/p&gt;</description></item><item><title>1.3 SwiftUI 中常见的异步 API</title><link>https://apolla.cc/posts/1.3-swiftui-%E4%B8%AD%E5%B8%B8%E8%A7%81%E7%9A%84%E5%BC%82%E6%AD%A5-api/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/1.3-swiftui-%E4%B8%AD%E5%B8%B8%E8%A7%81%E7%9A%84%E5%BC%82%E6%AD%A5-api/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="task"&gt;&lt;span&gt;.task&lt;/span&gt;&#10; &lt;a href="#task" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;SwiftUI 刚发布时，View 只有两个最基础的生命周期方法：&lt;code&gt;.onAppear&lt;/code&gt; 和 &lt;code&gt;.onDisAppear&lt;/code&gt;，iOS 14 又新增了 &lt;code&gt;.onChange&lt;/code&gt; 这样的修饰器，可以监听值的变化。不过上面的几个 API 都是在主线程调用的，iOS 15 新增的 &lt;code&gt;.task&lt;/code&gt; 修饰器赋予了 View 快速开启异步任务的能力。&lt;/p&gt;</description></item><item><title>2.1 任务</title><link>https://apolla.cc/posts/2.1-%E4%BB%BB%E5%8A%A1/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/2.1-%E4%BB%BB%E5%8A%A1/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="任务的创建"&gt;&lt;span&gt;任务的创建&lt;/span&gt;&#10; &lt;a href="#%e4%bb%bb%e5%8a%a1%e7%9a%84%e5%88%9b%e5%bb%ba" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;在前面的章节中，我们已经使用过 &lt;code&gt;Task&lt;/code&gt; 了，比如在同步环境 &lt;code&gt;viewDidLoad&lt;/code&gt; 方法中开启异步环境，或者使用 View 的 &lt;code&gt;.task&lt;/code&gt; 修饰器开启异步任务。还有 &lt;code&gt;async let&lt;/code&gt; 这样的语法糖，也会隐式地创建 task，方便我们写出简洁、安全的并发代码。&lt;/p&gt;</description></item><item><title>2.2 任务组</title><link>https://apolla.cc/posts/2.2-%E4%BB%BB%E5%8A%A1%E7%BB%84/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/2.2-%E4%BB%BB%E5%8A%A1%E7%BB%84/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="任务组的创建"&gt;&lt;span&gt;任务组的创建&lt;/span&gt;&#10; &lt;a href="#%e4%bb%bb%e5%8a%a1%e7%bb%84%e7%9a%84%e5%88%9b%e5%bb%ba" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;code&gt;async let&lt;/code&gt; 和 &lt;code&gt;Task&lt;/code&gt; 能够满足我们大多数的并发需求，但在某些更复杂的需求场景下，它们力所不及。比如这样的需求：我们先发起一个网络请求，拉取所有要下载的书籍并展示在列表上。然后，我们要实现“下载全部”的功能。&lt;/p&gt;</description></item><item><title>2.3 小结</title><link>https://apolla.cc/posts/2.3-%E5%B0%8F%E7%BB%93/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/2.3-%E5%B0%8F%E7%BB%93/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;p&gt;前面的章节中，我们学习了 &lt;code&gt;async let&lt;/code&gt;、&lt;code&gt;Task&lt;/code&gt; 以及 &lt;code&gt;TaskGroup&lt;/code&gt; 的使用。它们都可以帮助我们写出高质量的并发代码，也各有千秋。&lt;/p&gt;&#10;&lt;p&gt;借助于 &lt;code&gt;async let&lt;/code&gt; 这个语法糖，我们能够在不直接接触 &lt;code&gt;Task&lt;/code&gt; 或 &lt;code&gt;TaskGroup&lt;/code&gt; 的层面上开启异步任务，获取异步返回结果，因此 &lt;code&gt;async let&lt;/code&gt; 又叫“并发绑定”，顾名思义，为某个变量绑定了并发任务的异步返回结果。&lt;/p&gt;</description></item><item><title>3.1 异步序列</title><link>https://apolla.cc/posts/3.1-%E5%BC%82%E6%AD%A5%E5%BA%8F%E5%88%97/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/3.1-%E5%BC%82%E6%AD%A5%E5%BA%8F%E5%88%97/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="asyncsequence-简介"&gt;&lt;span&gt;AsyncSequence 简介&lt;/span&gt;&#10; &lt;a href="#asyncsequence-%e7%ae%80%e4%bb%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swift/asyncsequence" target="_blank" rel="external nofollow noopener noreferrer"&gt;AsyncSequence&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 是一个支持异步迭代的协议，它和 &lt;a href="https://developer.apple.com/documentation/swift/sequence" target="_blank" rel="external nofollow noopener noreferrer"&gt;Sequence&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 非常类似，同样支持使用 &lt;code&gt;for&lt;/code&gt;、&lt;code&gt;while&lt;/code&gt;、&lt;code&gt;repeat&lt;/code&gt; 循环访问子元素，也支持使用 &lt;code&gt;map&lt;/code&gt;、&lt;code&gt;filter&lt;/code&gt;、&lt;code&gt;reduce&lt;/code&gt; 等高阶函数，只不过这些操作都是异步的。&lt;/p&gt;</description></item><item><title>3.2 异步流</title><link>https://apolla.cc/posts/3.2-%E5%BC%82%E6%AD%A5%E6%B5%81/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/3.2-%E5%BC%82%E6%AD%A5%E6%B5%81/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="asyncstream-的使用"&gt;&lt;span&gt;AsyncStream 的使用&lt;/span&gt;&#10; &lt;a href="#asyncstream-%e7%9a%84%e4%bd%bf%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/swift/asyncstream" target="_blank" rel="external nofollow noopener noreferrer"&gt;AsyncStream&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt; 是一个遵循 &lt;code&gt;AsyncSequence&lt;/code&gt; 协议的结构体，借助于它，我们无需手动实现异步迭代器，就可以方便地构建异步序列。相比 &lt;code&gt;AsyncSequence&lt;/code&gt; ，它是一个实现异步序列的轻量级方案。&lt;/p&gt;</description></item><item><title>4.1 actor 的使用</title><link>https://apolla.cc/posts/4.1-actor-%E7%9A%84%E4%BD%BF%E7%94%A8/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/4.1-actor-%E7%9A%84%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="actor-并发模型"&gt;&lt;span&gt;Actor 并发模型&lt;/span&gt;&#10; &lt;a href="#actor-%e5%b9%b6%e5%8f%91%e6%a8%a1%e5%9e%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;img loading="lazy" src='https://apolla.cc/images/actor.png' alt="/images/actor.png"&gt;&lt;/p&gt;&#10;&lt;p&gt;actor 并发模型首次发表于 1973 年，是一个用于并发计算的数学模型。&lt;/p&gt;</description></item><item><title>4.2 @globalActor、@MainActor 的使用</title><link>https://apolla.cc/posts/4.2-@globalactor@mainactor-%E7%9A%84%E4%BD%BF%E7%94%A8/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/4.2-@globalactor@mainactor-%E7%9A%84%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="globalactor"&gt;&lt;span&gt;GlobalActor&lt;/span&gt;&#10; &lt;a href="#globalactor" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;状态隔离使得 actor 成为在并发环境中安全使用的引用类型，但它的作用范围仅限于单个 actor 类型。如果某些变量、函数分散在不同的地方，actor 无法将它们置于同一个隔离环境中。因此，Swift 引入了 &lt;a href="https://developer.apple.com/documentation/swift/globalactor" target="_blank" rel="external nofollow noopener noreferrer"&gt;GlobalActor 协议&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;，将状态隔离的概念扩展至全局范围。&lt;/p&gt;</description></item><item><title>流程控制</title><link>https://apolla.cc/posts/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/</link><pubDate>Mon, 08 Apr 2024 15:42:27 +0800</pubDate><guid>https://apolla.cc/posts/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h2 class="heading-element" id="条件控制"&gt;&lt;span&gt;条件控制&lt;/span&gt;&#10; &lt;a href="#%e6%9d%a1%e4%bb%b6%e6%8e%a7%e5%88%b6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;h3 class="heading-element" id="if-else"&gt;&lt;span&gt;if-else&lt;/span&gt;&#10; &lt;a href="#if-else" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;h3 class="heading-element" id="区别"&gt;&lt;span&gt;区别&lt;/span&gt;&#10; &lt;a href="#%e5%8c%ba%e5%88%ab" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;ol&gt;&#10;&lt;li&gt;if后面的条件的小括号可以省略&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;pre&gt;&lt;code&gt;if age &amp;gt; 22 {&#10;    print(&amp;#34;age &amp;gt; 22&amp;#34;)&#10;}else{&#10;    print(&amp;#34;age &amp;lt;= 22&amp;#34;)&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="while"&gt;&lt;span&gt;while&lt;/span&gt;&#10; &lt;a href="#while" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;var n = 10&#10;&#10;while n &amp;gt; 0 {&#10;&#10;    print(&amp;#34;n == \(n)&amp;#34;)&#10;&#10;    n -= 1&#10;&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="repeat-while"&gt;&lt;span&gt;repeat-while&lt;/span&gt;&#10; &lt;a href="#repeat-while" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;blockquote&gt;&#10;&lt;p&gt;相当于do-while&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>4.3 Operators 使用手册（三）</title><link>https://apolla.cc/posts/4.3-operators-%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C%E4%B8%89/</link><pubDate>Fri, 19 Apr 2024 15:20:06 +0800</pubDate><guid>https://apolla.cc/posts/4.3-operators-%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C%E4%B8%89/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="republish"&gt;&lt;span&gt;Republish&lt;/span&gt;&#10; &lt;a href="#republish" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="switchtolatest"&gt;&lt;span&gt;switchToLatest&lt;/span&gt;&#10; &lt;a href="#switchtolatest" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;字面意思就是切换到最近的 publisher，这比较难那理解，我们结合代码来看。&lt;/p&gt;</description></item><item><title>1.1 Combine 概览</title><link>https://apolla.cc/posts/1.1-combine-%E6%A6%82%E8%A7%88/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/1.1-combine-%E6%A6%82%E8%A7%88/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;p&gt;WWDC 2019 上与声明式框架 SwiftUI 一同发布的，还有同等重要的响应式框架 Combine，它用于处理（异步）事件和数据流。随着 Concurrency 框架的发布和 SwiftUI 的不断完善，可能在实际开发中 Combine 直接露面的概率会越来越小。但它是 SwiftUI 简洁高效的基石，了解它有助于更好地理解和使用 SwiftUI。&lt;/p&gt;&#10;&lt;p&gt;Combine 框架包含三个核心概念：Publisher、Subscriber、Operator。&lt;/p&gt;</description></item><item><title>2.1 Publisher 的基本使用（一）</title><link>https://apolla.cc/posts/2.1-publisher-%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E4%B8%80/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/2.1-publisher-%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E4%B8%80/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;p&gt;在前面的章节中，实际上我们已经多次使用过 Publisher，只是我们不曾察觉。比如我们之前使用的 ObservedObject，它的定义如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;@propertyWrapper @frozen struct ObservedObject&amp;lt;ObjectType&amp;gt; where ObjectType : ObservableObject&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;其中的泛型 ObjectType 遵循 &lt;strong&gt;ObservableObject&lt;/strong&gt; 协议，该协议的定义中只有一个属性：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;// A publisher that emits before the object has changed.&#10;var objectWillChange: Self.ObjectWillChangePublisher&#10;&#10;// The type of publisher that emits before the object has changed.&#10;associatedtype ObjectWillChangePublisher : Publisher = ObservableObjectPublisher&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;其中的关联类型 ObjectWillChangePublisher 正是遵循 &lt;strong&gt;Publisher&lt;/strong&gt; 协议。也就是说，当我们使用 ObservedObject 时，系统会隐式地生成 objectWillChange 这样一个 Publisher，一旦遵循 ObservableObject 的 class 中标记为 @Published 的属性值发生变化，就会通过该 Publisher 告知相关的对象。&lt;/p&gt;</description></item><item><title>2.2 Publisher 的基本使用（二）</title><link>https://apolla.cc/posts/2.2-publisher-%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E4%BA%8C/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/2.2-publisher-%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E4%BA%8C/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="anypublisher"&gt;&lt;span&gt;AnyPublisher&lt;/span&gt;&#10; &lt;a href="#anypublisher" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;前面视图部分的内容我们提到过，有些情况下， View 为了避免在传参或返回时类型不一致从而无法通过编译时，可以使用 AnyView 来作类型擦除，以此使得视图变得通用。&lt;/p&gt;</description></item><item><title>2.3 特殊的 Publisher：Subject</title><link>https://apolla.cc/posts/2.3-%E7%89%B9%E6%AE%8A%E7%9A%84-publishersubject/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/2.3-%E7%89%B9%E6%AE%8A%E7%9A%84-publishersubject/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;p&gt;Subject 协议继承自 Publisher 协议，遵循 Subject 协议的 Publisher 是一种特定类型的 Publisher，它有如下方法：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;/** Subject 协议方法 */&#10;&#10;/** 向订阅者发送数据 */&#10;// 发送指定值&#10;func send(Self.Output)&#10;// 发送空值（此时 Output 类型为 Void）&#10;func send()&#10;&#10;/** 向订阅者发送生命周期事件 */&#10;// 发送 subscription 实例&#10;func send(subscription: Subscription)&#10;&#10;// 发送 completion 信号&#10;func send(completion: Subscribers.Completion&amp;lt;Self.Failure&amp;gt;)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Subject 的特别之处在于，我们可以手动调用 send 方法向数据流注入新的值。&lt;/p&gt;</description></item><item><title>3.1 Subscriber 的详细介绍</title><link>https://apolla.cc/posts/3.1-subscriber-%E7%9A%84%E8%AF%A6%E7%BB%86%E4%BB%8B%E7%BB%8D/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/3.1-subscriber-%E7%9A%84%E8%AF%A6%E7%BB%86%E4%BB%8B%E7%BB%8D/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="订阅行为的详细过程"&gt;&lt;span&gt;订阅行为的详细过程&lt;/span&gt;&#10; &lt;a href="#%e8%ae%a2%e9%98%85%e8%a1%8c%e4%b8%ba%e7%9a%84%e8%af%a6%e7%bb%86%e8%bf%87%e7%a8%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;前面我们介绍过了，&lt;strong&gt;Subscriber&lt;/strong&gt; 是一个协议，和 Publisher 遥相呼应，前者接收数据（Input、Failure），后者发送数据（Output、Failure)，数据类型必须保持一致。&lt;/p&gt;</description></item><item><title>3.2 内建 Subscriber 的使用</title><link>https://apolla.cc/posts/3.2-%E5%86%85%E5%BB%BA-subscriber-%E7%9A%84%E4%BD%BF%E7%94%A8/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/3.2-%E5%86%85%E5%BB%BA-subscriber-%E7%9A%84%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;p&gt;Combine 内建了两个 Subscriber：sink、assign，SwiftUI 框架中有一个内建的 Subscriber：onReceive。&lt;/p&gt;&#10;&lt;h1 class="heading-element" id="sink"&gt;&lt;span&gt;sink&lt;/span&gt;&#10; &lt;a href="#sink" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;关于 sink，前面我们已经介绍过了。这里只做复习：它的作用是生成订阅者来接收发布者发送的数据，该订阅者在 receiveCompletion 闭包中返回事件结束的消息，在 receiveValue 闭包中返回接收到的数据值。&lt;/p&gt;</description></item><item><title>4.1 Operators 使用手册（一）</title><link>https://apolla.cc/posts/4.1-operators-%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C%E4%B8%80/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/4.1-operators-%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C%E4%B8%80/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;p&gt;我们可以在数据流的管道中，使用 Operators 对上游的数据进行各种操作，然后再发送给下游的订阅者，这就是 Operators 的作用。&lt;/p&gt;&#10;&lt;p&gt;Combine 提供了大量的 Operators 给我们使用，鉴于内容过多，笔者将其分为三个小节。&lt;/p&gt;&#10;&lt;p&gt;后续内容用了部分&lt;strong&gt;弹珠图&lt;/strong&gt;来加深理解，有兴趣的读者可以进一步了解：&lt;a href="https://rx-marbles-online.herokuapp.com/" target="_blank" rel="external nofollow noopener noreferrer"&gt;Marble Diagrams&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;。&lt;/p&gt;</description></item><item><title>4.2 Operators 使用手册（二）</title><link>https://apolla.cc/posts/4.2-operators-%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C%E4%BA%8C/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/4.2-operators-%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C%E4%BA%8C/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="mathematic"&gt;&lt;span&gt;Mathematic&lt;/span&gt;&#10; &lt;a href="#mathematic" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="maxtrymax"&gt;&lt;span&gt;max、tryMax&lt;/span&gt;&#10; &lt;a href="#maxtrymax" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;&lt;img loading="lazy" src='https://apolla.cc/images/max.png' alt="/images/max.png"&gt;&lt;/p&gt;</description></item><item><title>函数</title><link>https://apolla.cc/posts/%E5%87%BD%E6%95%B0/</link><pubDate>Mon, 08 Apr 2024 17:57:25 +0800</pubDate><guid>https://apolla.cc/posts/%E5%87%BD%E6%95%B0/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h2 class="heading-element" id="函数定义"&gt;&lt;span&gt;函数定义&lt;/span&gt;&#10; &lt;a href="#%e5%87%bd%e6%95%b0%e5%ae%9a%e4%b9%89" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code&gt;//有返回值&#10;func sum(v1: Int, v2: Int) -&amp;gt; Int {&#10;    return v1 &amp;#43; v2&#10;}&#10;sum(v1: 10, v2: 20)&#10;&#10;//无返回值&#10;func sayHello(){&#10;    print(&amp;#34;Hello&amp;#34;)&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;blockquote&gt;&#10;&lt;p&gt;形参默认是let 也只能是let&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>5 Swift 5.9 新特性</title><link>https://apolla.cc/posts/5-swift-5.9-%E6%96%B0%E7%89%B9%E6%80%A7/</link><pubDate>Fri, 19 Apr 2024 15:20:06 +0800</pubDate><guid>https://apolla.cc/posts/5-swift-5.9-%E6%96%B0%E7%89%B9%E6%80%A7/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;注：Swift 5.9 目前并未正式发布，本文会在正式发布后再次更新。&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h1 class="heading-element" id="if-和-switch-表达式se-0380"&gt;&lt;span&gt;if 和 switch 表达式（&lt;a href="https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md" target="_blank" rel="external nofollow noopener noreferrer"&gt;SE-0380&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;）&lt;/span&gt;&#10; &lt;a href="#if-%e5%92%8c-switch-%e8%a1%a8%e8%be%be%e5%bc%8fse-0380" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;pre&gt;&lt;code&gt;let s1 = if Bool.random() { &amp;#34;YES&amp;#34; } else { &amp;#34;NO&amp;#34; }&#10;&#10;let s2 = switch Bool.random() {&#10;case true: &amp;#34;YES&amp;#34;&#10;case false: &amp;#34;NO&amp;#34;&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;h1 class="heading-element" id="值和类型参数包se-0393"&gt;&lt;span&gt;值和类型参数包（&lt;a href="https://github.com/apple/swift-evolution/blob/main/proposals/0393-parameter-packs.md" target="_blank" rel="external nofollow noopener noreferrer"&gt;SE-0393&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;）&lt;/span&gt;&#10; &lt;a href="#%e5%80%bc%e5%92%8c%e7%b1%bb%e5%9e%8b%e5%8f%82%e6%95%b0%e5%8c%85se-0393" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;值和类型参数，实现了对不同类型的值和类型的抽象化，这是在 Swift 中实现可变泛型的第一步。&lt;/p&gt;</description></item><item><title>枚举</title><link>https://apolla.cc/posts/%E6%9E%9A%E4%B8%BE/</link><pubDate>Tue, 09 Apr 2024 14:42:34 +0800</pubDate><guid>https://apolla.cc/posts/%E6%9E%9A%E4%B8%BE/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h2 class="heading-element" id="常规用法"&gt;&lt;span&gt;常规用法&lt;/span&gt;&#10; &lt;a href="#%e5%b8%b8%e8%a7%84%e7%94%a8%e6%b3%95" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code&gt;enum accountType {&#10;    case email&#10;    case phone&#10;}&#10;accountType.email&#10;accountType.phone&lt;/code&gt;&lt;/pre&gt;&lt;h2 class="heading-element" id="关联值"&gt;&lt;span&gt;关联值&lt;/span&gt;&#10; &lt;a href="#%e5%85%b3%e8%81%94%e5%80%bc" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;blockquote&gt;&#10;&lt;p&gt;有时将枚举的成员值跟其他类型的值关联存储在一起，会非常有用&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>4 Swift 5.8 新特性</title><link>https://apolla.cc/posts/4-swift-5.8-%E6%96%B0%E7%89%B9%E6%80%A7/</link><pubDate>Fri, 31 Mar 2023 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/4-swift-5.8-%E6%96%B0%E7%89%B9%E6%80%A7/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="闭包内隐式调用-selfse-0365"&gt;&lt;span&gt;闭包内隐式调用 self（&lt;a href="https://github.com/apple/swift-evolution/blob/main/proposals/0365-implicit-self-weak-capture.md" target="_blank" rel="external nofollow noopener noreferrer"&gt;SE-0365&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;）&lt;/span&gt;&#10; &lt;a href="#%e9%97%ad%e5%8c%85%e5%86%85%e9%9a%90%e5%bc%8f%e8%b0%83%e7%94%a8-selfse-0365" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;pre&gt;&lt;code&gt;class Timer {&#10; var eventHandler: (() -&amp;gt; Void)?&#10; &#10; func start() {&#10; DispatchQueue.main.asyncAfter(deadline: .now() &amp;#43; 5) {&#10; self.eventHandler?()&#10; }&#10; }&#10;}&#10;&#10;class ViewController {&#10; let timer = Timer()&#10; &#10; init() {&#10; timer.eventHandler = { [weak self] in&#10; guard let self else { return }&#10; // 如果已使用 [weak self] 捕获了 self 并且解包&#10; // 则无需显式调用 self&#10; doSomething()&#10; }&#10; timer.start()&#10; }&#10; &#10; func doSomething() {&#10; print(&amp;#34;Doing something...&amp;#34;)&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;h1 class="heading-element" id="集合类型支持向下类型转换"&gt;&lt;span&gt;集合类型支持向下类型转换&lt;/span&gt;&#10; &lt;a href="#%e9%9b%86%e5%90%88%e7%b1%bb%e5%9e%8b%e6%94%af%e6%8c%81%e5%90%91%e4%b8%8b%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;pre&gt;&lt;code&gt;class A {}&#10;class A1: A {}&#10;class A2: A {}&#10;&#10;func downcast(arr: [A]) {&#10; switch arr {&#10; case let a1 as [A1]:&#10; print(&amp;#34;arr is [A1]: \(a1)&amp;#34;)&#10; case let a2 as [A2]:&#10; print(&amp;#34;arr is [A2]: \(a2)&amp;#34;)&#10; default:&#10; print(&amp;#34;Unknown collection type&amp;#34;)&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Swift 5.8 以前，以上的代码无法通过编译。需要这样写才可以：&lt;/p&gt;</description></item><item><title>3 Swift 中的 Result builders</title><link>https://apolla.cc/posts/3-swift-%E4%B8%AD%E7%9A%84-result-builders/</link><pubDate>Tue, 30 Aug 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/3-swift-%E4%B8%AD%E7%9A%84-result-builders/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;p&gt;在以往的命令式编程中，通常这样构建视图：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;let label = UILabel()&#10;label.frame = self.view.bounds&#10;self.view.addSubview(label)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;但 SwiftUI 采用的是声明式语法：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;VStack {&#10; Text(&amp;#34;1&amp;#34;)&#10;&#9;List {&#10; Text(&amp;#34;&amp;#34;)&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;显而易见，SwiftUI 比 UIKit 要简洁地多，我们可以轻松地将不同的视图组合成复杂的界面。这得益于 Result builders（结果构造器），它使 SwiftUI 成为了特定领域语言（DSL）。&lt;/p&gt;</description></item><item><title>1 Swift 5.7 新特性</title><link>https://apolla.cc/posts/1-swift-5.7-%E6%96%B0%E7%89%B9%E6%80%A7/</link><pubDate>Fri, 15 Jul 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/1-swift-5.7-%E6%96%B0%E7%89%B9%E6%80%A7/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="简化的可选绑定se-0345"&gt;&lt;span&gt;简化的可选绑定（&lt;a href="https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md" target="_blank" rel="external nofollow noopener noreferrer"&gt;SE-0345&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;）&lt;/span&gt;&#10; &lt;a href="#%e7%ae%80%e5%8c%96%e7%9a%84%e5%8f%af%e9%80%89%e7%bb%91%e5%ae%9ase-0345" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;对可选类型解包时无需显式绑定，写法更简洁：&lt;/p&gt;</description></item><item><title>2 Swift 中的不透明类型和存在类型</title><link>https://apolla.cc/posts/2-swift-%E4%B8%AD%E7%9A%84%E4%B8%8D%E9%80%8F%E6%98%8E%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%AD%98%E5%9C%A8%E7%B1%BB%E5%9E%8B/</link><pubDate>Wed, 13 Jul 2022 08:00:00 +0800</pubDate><guid>https://apolla.cc/posts/2-swift-%E4%B8%AD%E7%9A%84%E4%B8%8D%E9%80%8F%E6%98%8E%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%AD%98%E5%9C%A8%E7%B1%BB%E5%9E%8B/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;br&gt;&#10;&lt;h1 class="heading-element" id="不透明类型some-关键字"&gt;&lt;span&gt;不透明类型、some 关键字&lt;/span&gt;&#10; &lt;a href="#%e4%b8%8d%e9%80%8f%e6%98%8e%e7%b1%bb%e5%9e%8bsome-%e5%85%b3%e9%94%ae%e5%ad%97" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;&lt;code&gt;some&lt;/code&gt; 关键字由 Swift 5.1 引入，它用来修饰某个协议，使之成为&lt;strong&gt;不透明类型&lt;/strong&gt;。&lt;/p&gt;</description></item><item><title>MemoryLayout</title><link>https://apolla.cc/posts/memorylayout/</link><pubDate>Tue, 09 Apr 2024 14:44:05 +0800</pubDate><guid>https://apolla.cc/posts/memorylayout/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;blockquote&gt;&#10;&lt;p&gt;可以使用MemoryLayout获取数据类型占用的内存大小&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;pre&gt;&lt;code&gt;enum Password {&#10;&#10;    case number(Int, Int, Int, Int) // 32&#10;&#10;    case other&#10;&#10;}&#10;&#10;var pwd = Password.number(5, 6, 4, 7) // 33&#10;pwd = .other // 0&#10;&#10;MemoryLayout&amp;lt;Password&amp;gt;.size // 33&#10;MemoryLayout&amp;lt;Password&amp;gt;.stride // 40&#10;MemoryLayout&amp;lt;Password&amp;gt;.alignment // 8&lt;/code&gt;&lt;/pre&gt;</description></item><item><title>可选项</title><link>https://apolla.cc/posts/%E5%8F%AF%E9%80%89%E9%A1%B9/</link><pubDate>Thu, 20 Jun 2024 20:52:48 +0800</pubDate><guid>https://apolla.cc/posts/%E5%8F%AF%E9%80%89%E9%A1%B9/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;ul&gt;&#10;&lt;li&gt;可选项，一般也叫可选类型，它允许将值设置为nil&lt;/li&gt;&#10;&lt;li&gt;在类型名称后面加个问号 ? 来定义一个可选项&lt;/li&gt;&#10;&lt;li&gt;本质上是enum类型 包含一个none和一个关联属性&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code&gt;var age: Int?&#10;print(age)&lt;/code&gt;&lt;/pre&gt;&lt;blockquote&gt;&#10;&lt;p&gt;例如 下面代码 用于判断数组越界 如果越界就返回空&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;pre&gt;&lt;code&gt;var ages = [10,15,3,9]&#10;&#10; &#10;&#10;func getAges(_ index:Int) -&amp;gt; Int? {&#10;    if index &amp;lt; 0 || index &amp;gt; ages.count {&#10;        return nil&#10;    }&#10;    return ages[index]&#10;}&#10;&#10;print(getAges(1)) //Optional(15)&#10;print(getAges(-1)) //nil&#10;print(getAges(16)) //nil&lt;/code&gt;&lt;/pre&gt;&lt;h2 class="heading-element" id="强制解包"&gt;&lt;span&gt;强制解包&lt;/span&gt;&#10; &lt;a href="#%e5%bc%ba%e5%88%b6%e8%a7%a3%e5%8c%85" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;可选项是对其他类型的一层包装，可以将它理解为一个盒子&lt;/li&gt;&#10;&lt;li&gt;如果为nil，那么它是个空盒子&lt;/li&gt;&#10;&lt;li&gt;如果不为nil，那么盒子里装的是：被包装类型的数据&lt;/li&gt;&#10;&lt;li&gt;如果要从可选项中取出被包装的数据（将盒子里装的东西取出来），需要使用感叹号 ! 进行强制解包&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;如果对值为nil的可选项（空盒子）进行强制解包，将会产生运行时错误（Fatal error: Unexpectedly found nil while unwrapping an Optional value）&lt;/strong&gt;&#10;!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code&gt;var age:Int?&#10;age = 10&#10;var nums = age! &amp;#43; 20&lt;/code&gt;&lt;/pre&gt;&lt;h2 class="heading-element" id="可选项绑定letvar-保护"&gt;&lt;span&gt;可选项绑定（let、var 保护）&lt;/span&gt;&#10; &lt;a href="#%e5%8f%af%e9%80%89%e9%a1%b9%e7%bb%91%e5%ae%9aletvar-%e4%bf%9d%e6%8a%a4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;可以使用可选项绑定来判断可选项是否包含值&lt;/li&gt;&#10;&lt;li&gt;如果包含就自动解包，把值赋给一个临时的常量(let)或者变量(var)，并返回true，否则返回false&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code&gt;if let number = Int(&amp;#34;123&amp;#34;){&#10;&#10;    print(&amp;#34;字符串转换整数成功:\(number)&amp;#34;)&#10;&#10;    // number是强制解包之后的Int值&#10;&#10;    // number作用域仅限于这个大括号&#10;&#10;} else {&#10;&#10;    print(&amp;#34;字符串转换整数失败&amp;#34;)&#10;&#10;}&#10;&#10;//字符串转换整数成功:123&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="可选项绑定在while中的用法"&gt;&lt;span&gt;可选项绑定在while中的用法&lt;/span&gt;&#10; &lt;a href="#%e5%8f%af%e9%80%89%e9%a1%b9%e7%bb%91%e5%ae%9a%e5%9c%a8while%e4%b8%ad%e7%9a%84%e7%94%a8%e6%b3%95" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;//遍历数组，将遇到的正数都加起来，如果遇到负数或者非数字，停止遍历&#10;&#10;var strs = [&amp;#34;10&amp;#34;,&amp;#34;20&amp;#34;,&amp;#34;abc&amp;#34;,&amp;#34;-20&amp;#34;,&amp;#34;30&amp;#34;]&#10;&#10;var index = 0&#10;&#10;var sum = 0&#10;&#10;while let num = Int(strs[index]),num &amp;gt; 0 {&#10;&#10;    sum &amp;#43;= num&#10;&#10;    index &amp;#43;= 1&#10;&#10;}&#10;&#10;print(sum)&lt;/code&gt;&lt;/pre&gt;&lt;h2 class="heading-element" id="隐式解包尽量少使用"&gt;&lt;span&gt;隐式解包(尽量少使用)&lt;/span&gt;&#10; &lt;a href="#%e9%9a%90%e5%bc%8f%e8%a7%a3%e5%8c%85%e5%b0%bd%e9%87%8f%e5%b0%91%e4%bd%bf%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;在某些情况下，可选项一旦被设定值之后，就会一直拥有值&lt;/li&gt;&#10;&lt;li&gt;在这种情况下，可以去掉检查，也不必每次访问的时候都进行解包，因为它能确定每次访问的时候都有值&lt;/li&gt;&#10;&lt;li&gt;可以在类型后面加个感叹号!定义一个隐式解包的可选项&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code&gt;let num1: Int! = 10&#10;let num2: Int = num1&#10;if num1 != nil {&#10;    print(num1 &amp;#43; 6)// 16&#10;}&#10;&#10;if let num3 = num1 {&#10;    print(num3)&#10;}&lt;/code&gt;&lt;/pre&gt;</description></item><item><title>空合并运算符??</title><link>https://apolla.cc/posts/%E7%A9%BA%E5%90%88%E5%B9%B6%E8%BF%90%E7%AE%97%E7%AC%A6/</link><pubDate>Tue, 09 Apr 2024 16:16:57 +0800</pubDate><guid>https://apolla.cc/posts/%E7%A9%BA%E5%90%88%E5%B9%B6%E8%BF%90%E7%AE%97%E7%AC%A6/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h2 class="heading-element" id="描述"&gt;&lt;span&gt;描述&lt;/span&gt;&#10; &lt;a href="#%e6%8f%8f%e8%bf%b0" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;a?? b&lt;/li&gt;&#10;&lt;li&gt;a 是可选项&lt;/li&gt;&#10;&lt;li&gt;b 是可选项 或者 不是可选项&lt;/li&gt;&#10;&lt;li&gt;b 跟 a 的存储类型必须相同&lt;/li&gt;&#10;&lt;li&gt;如果 a 不为nil,就返回 a&lt;/li&gt;&#10;&lt;li&gt;如果a 为nil,就返回 b&lt;/li&gt;&#10;&lt;li&gt;如果 b 不是可选项，返回 a 时会自动解包&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;举例说明&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>结构体和类</title><link>https://apolla.cc/posts/%E7%BB%93%E6%9E%84%E4%BD%93%E5%92%8C%E7%B1%BB/</link><pubDate>Wed, 10 Apr 2024 17:17:06 +0800</pubDate><guid>https://apolla.cc/posts/%E7%BB%93%E6%9E%84%E4%BD%93%E5%92%8C%E7%B1%BB/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;ul&gt;&#10;&lt;li&gt;在 Swift 标准库中，绝大多数的公开类型都是结构体，而枚举和类只占很小一部分&lt;/li&gt;&#10;&lt;li&gt;比如Bool、 Int、 Double、 String、 Array、 Dictionary等常见类型都是结构体&lt;/li&gt;&#10;&lt;li&gt;所有的结构体都有一个编译器自动生成的初始化器（ initializer，初始化方法、构造器、构造方法）&lt;/li&gt;&#10;&lt;li&gt;在第⑥行调用的，可以传入所有成员值，用以初始化所有成员（存储属性， Stored Property）&lt;/li&gt;&#10;&lt;li&gt;编译器会根据情况，可能会为结构体生成多个初始化器，宗旨是：保证所有成员都有初始值&lt;/li&gt;&#10;&lt;li&gt;一旦在定义结构体时自定义了初始化器，编译器就不会再帮它自动生成其他初始化器&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code&gt;struct Date {&#10;    var year:Int&#10;    var month:Int&#10;    var day:Int&#10;}&#10;&#10;var deate = Date(year: 2024, month: 3, day: 24)&lt;/code&gt;&lt;/pre&gt;&lt;h2 class="heading-element" id="类"&gt;&lt;span&gt;类&lt;/span&gt;&#10; &lt;a href="#%e7%b1%bb" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;类的定义和结构体类似，但编译器并没有为类自动生成可以传入成员值的初始化器&lt;/li&gt;&#10;&lt;li&gt;如果类的所有成员都在定义的时候指定了初始值，编译器会为类生成无参的初始化器&lt;/li&gt;&#10;&lt;li&gt;成员的初始化是在这个初始化器中完成的&#10;!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="结构体与类的本质区别"&gt;&lt;span&gt;结构体与类的本质区别&lt;/span&gt;&#10; &lt;a href="#%e7%bb%93%e6%9e%84%e4%bd%93%e4%b8%8e%e7%b1%bb%e7%9a%84%e6%9c%ac%e8%b4%a8%e5%8c%ba%e5%88%ab" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;结构体是值类型（枚举也是值类型），类是引用类型（指针类型）&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;!&lt;/p&gt;</description></item><item><title>闭包</title><link>https://apolla.cc/posts/%E9%97%AD%E5%8C%85/</link><pubDate>Thu, 11 Apr 2024 17:12:41 +0800</pubDate><guid>https://apolla.cc/posts/%E9%97%AD%E5%8C%85/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;blockquote&gt;&#10;&lt;p&gt;在Swift中，可以通过func定义一个函数，也可以通过闭包表达式定义一个函数&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 class="heading-element" id="闭包表达式"&gt;&lt;span&gt;闭包表达式&lt;/span&gt;&#10; &lt;a href="#%e9%97%ad%e5%8c%85%e8%a1%a8%e8%be%be%e5%bc%8f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code&gt;{&#10;(参数列表) -&amp;gt; 返回值类型 in&#10;函数体代码&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;举例：&lt;/p&gt;</description></item><item><title>属性</title><link>https://apolla.cc/posts/%E5%B1%9E%E6%80%A7/</link><pubDate>Fri, 12 Apr 2024 10:29:42 +0800</pubDate><guid>https://apolla.cc/posts/%E5%B1%9E%E6%80%A7/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;p&gt;Swift中跟实例相关的属性可以分为2大类&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;存储属性（ Stored Property）&#10;&lt;ul&gt;&#10;&lt;li&gt;类似于成员变量这个概念&lt;/li&gt;&#10;&lt;li&gt;存储在实例的内存中&lt;/li&gt;&#10;&lt;li&gt;结构体、类可以定义存储属性&lt;/li&gt;&#10;&lt;li&gt;枚举不可以定义存储属性&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;计算属性（ Computed Property）&#10;&lt;ul&gt;&#10;&lt;li&gt;本质就是方法（函数）&lt;/li&gt;&#10;&lt;li&gt;不占用实例的内存&lt;/li&gt;&#10;&lt;li&gt;枚举、结构体、类都可以定义计算属性&#10;!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="存储属性"&gt;&lt;span&gt;存储属性&lt;/span&gt;&#10; &lt;a href="#%e5%ad%98%e5%82%a8%e5%b1%9e%e6%80%a7" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;关于存储属性， Swift有个明确的规定&lt;/p&gt;</description></item><item><title>方法</title><link>https://apolla.cc/posts/%E6%96%B9%E6%B3%95/</link><pubDate>Fri, 12 Apr 2024 10:45:16 +0800</pubDate><guid>https://apolla.cc/posts/%E6%96%B9%E6%B3%95/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;ul&gt;&#10;&lt;li&gt;枚举、结构体、类都可以定义实例方法、类型方法&#10;&lt;ul&gt;&#10;&lt;li&gt;实例方法（ Instance Method）： 通过实例对象调用&lt;/li&gt;&#10;&lt;li&gt;类型方法（ Type Method）： 通过类型调用，用static或者class关键字定义&#10;!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="mutating"&gt;&lt;span&gt;mutating&lt;/span&gt;&#10; &lt;a href="#mutating" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;结构体和枚举是值类型，默认情况下，值类型的属性不能被自身的实例方法修改&lt;/li&gt;&#10;&lt;li&gt;在func关键字前加mutating可以允许这种修改行为&#10;!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="discardableresult"&gt;&lt;span&gt;@discardableResult&lt;/span&gt;&#10; &lt;a href="#discardableresult" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;blockquote&gt;&#10;&lt;p&gt;在func前面加个&lt;code&gt;@discardableResult&lt;/code&gt; ， 可以消除：函数调用后返回值未被使用的警告⚠&#10;!&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>下标</title><link>https://apolla.cc/posts/%E4%B8%8B%E6%A0%87/</link><pubDate>Fri, 12 Apr 2024 10:51:32 +0800</pubDate><guid>https://apolla.cc/posts/%E4%B8%8B%E6%A0%87/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;blockquote&gt;&#10;&lt;p&gt;注： 暂未发现应用场景&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 class="heading-element" id="不同的下标概念"&gt;&lt;span&gt;不同的下标概念&lt;/span&gt;&#10; &lt;a href="#%e4%b8%8d%e5%90%8c%e7%9a%84%e4%b8%8b%e6%a0%87%e6%a6%82%e5%bf%b5" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;!&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="下标的细节"&gt;&lt;span&gt;下标的细节&lt;/span&gt;&#10; &lt;a href="#%e4%b8%8b%e6%a0%87%e7%9a%84%e7%bb%86%e8%8a%82" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;!&#10;!&lt;/p&gt;</description></item><item><title>继承</title><link>https://apolla.cc/posts/%E7%BB%A7%E6%89%BF/</link><pubDate>Fri, 12 Apr 2024 11:16:39 +0800</pubDate><guid>https://apolla.cc/posts/%E7%BB%A7%E6%89%BF/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;ul&gt;&#10;&lt;li&gt;值类型（枚举、结构体）不支持继承，只有类支持继承&lt;/li&gt;&#10;&lt;li&gt;没有父类的类，称为：基类&#10;&lt;ul&gt;&#10;&lt;li&gt;Swift并没有像OC、 Java那样的规定：任何类最终都要继承自某个基类&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;子类可以重写父类的下标、 方法、 属性，重写必须加上override关键字&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="重写类型方法下标"&gt;&lt;span&gt;重写类型方法、下标&lt;/span&gt;&#10; &lt;a href="#%e9%87%8d%e5%86%99%e7%b1%bb%e5%9e%8b%e6%96%b9%e6%b3%95%e4%b8%8b%e6%a0%87" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;被class修饰的类型方法、下标， 允许被子类重写&lt;/li&gt;&#10;&lt;li&gt;被static修饰的类型方法、下标， 不允许被子类重写&#10;!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="重写属性"&gt;&lt;span&gt;重写属性&lt;/span&gt;&#10; &lt;a href="#%e9%87%8d%e5%86%99%e5%b1%9e%e6%80%a7" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;子类可以将父类的属性（存储、计算）重写为计算属性&lt;/li&gt;&#10;&lt;li&gt;子类不可以将父类属性重写为存储属性&lt;/li&gt;&#10;&lt;li&gt;只能重写var属性，不能重写let属性&lt;/li&gt;&#10;&lt;li&gt;重写时，属性名、类型要一致&lt;/li&gt;&#10;&lt;li&gt;子类重写后的属性权限 不能小于 父类属性的权限&#10;&lt;ul&gt;&#10;&lt;li&gt;如果父类属性是只读的，那么子类重写后的属性可以是只读的、也可以是可读写的&lt;/li&gt;&#10;&lt;li&gt;如果父类属性是可读写的，那么子类重写后的属性也必须是可读写的&#10;!&#10;!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="重写类型属性"&gt;&lt;span&gt;重写类型属性&lt;/span&gt;&#10; &lt;a href="#%e9%87%8d%e5%86%99%e7%b1%bb%e5%9e%8b%e5%b1%9e%e6%80%a7" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;被class修饰的计算类型属性， 可以被子类重写&lt;/li&gt;&#10;&lt;li&gt;被static修饰的类型属性（存储、计算） ， 不可以被子类重写&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="属性观察器"&gt;&lt;span&gt;属性观察器&lt;/span&gt;&#10; &lt;a href="#%e5%b1%9e%e6%80%a7%e8%a7%82%e5%af%9f%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;可以在子类中为父类属性（除了只读计算属性、 let属性）增加属性观察器&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="final"&gt;&lt;span&gt;final&lt;/span&gt;&#10; &lt;a href="#final" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;被final修饰的方法、下标、属性，禁止被重写&lt;/li&gt;&#10;&lt;li&gt;被final修饰的类，禁止被继承&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>协议（ Protocol）</title><link>https://apolla.cc/posts/%E5%8D%8F%E8%AE%AE-protocol/</link><pubDate>Mon, 15 Apr 2024 15:11:37 +0800</pubDate><guid>https://apolla.cc/posts/%E5%8D%8F%E8%AE%AE-protocol/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;协议&lt;/em&gt; 定义了一个蓝图，规定了用来实现某一特定任务或者功能的方法、属性，以及其他需要的东西。类、结构体或枚举都可以遵循协议，并为协议定义的这些要求提供具体实现。某个类型能够满足某个协议的要求，就可以说该类型_遵循_这个协议。&lt;/p&gt;&#10;&lt;p&gt;除了遵循协议的类型必须实现的要求外，还可以对协议进行扩展，通过扩展来实现一部分要求或者实现一些附加功能，这样遵循协议的类型就能够使用这些功能。&lt;/p&gt;</description></item><item><title>杂碎知识</title><link>https://apolla.cc/posts/%E6%9D%82%E7%A2%8E%E7%9F%A5%E8%AF%86/</link><pubDate>Fri, 12 Apr 2024 14:43:57 +0800</pubDate><guid>https://apolla.cc/posts/%E6%9D%82%E7%A2%8E%E7%9F%A5%E8%AF%86/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h2 class="heading-element" id="anyanyobject"&gt;&lt;span&gt;Any、AnyObject&lt;/span&gt;&#10; &lt;a href="#anyanyobject" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;Swift提供了2种特殊的类型： Any、 AnyObject&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Any：可以代表任意类型（枚举、结构体、类，也包括函数类型）&lt;/li&gt;&#10;&lt;li&gt;AnyObject：可以代表任意类类型（在协议后面写上: AnyObject代表只有类能遵守这个协议）&#10;&lt;ul&gt;&#10;&lt;li&gt;在协议后面写上: class也代表只有类能遵守这个协议&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="is-as-as-as"&gt;&lt;span&gt;is、 as?、 as!、 as&lt;/span&gt;&#10; &lt;a href="#is-as-as-as" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;is用来判断是否为某种类型&lt;/li&gt;&#10;&lt;li&gt;as用来做强制类型转换&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;as&lt;/p&gt;&#10;&lt;p&gt;1）从派生类转换为基类，向上转型（upcasts）&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;</description></item><item><title>Error</title><link>https://apolla.cc/posts/error/</link><pubDate>Mon, 15 Apr 2024 09:22:49 +0800</pubDate><guid>https://apolla.cc/posts/error/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h2 class="heading-element" id="自定义错误"&gt;&lt;span&gt;自定义错误&lt;/span&gt;&#10; &lt;a href="#%e8%87%aa%e5%ae%9a%e4%b9%89%e9%94%99%e8%af%af" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;Swift中可以通过Error协议自定义运行时的错误信息&lt;/li&gt;&#10;&lt;li&gt;函数内部通过throw抛出自定义Error，可能会抛出Error的函数必须加上throws声明&lt;/li&gt;&#10;&lt;li&gt;需要使用try调用可能会抛出Error的函数&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;pre&gt;&lt;code&gt;enum SomeError : Error {&#10;    case illegalArg(String)&#10;    case outOfBounds(Int, Int)&#10;    case outOfMemory&#10;&#10;}&#10;&#10;func divide(_ num1: Int, _ num2: Int) throws -&amp;gt; Int {&#10;&#10;    if num2 == 0 {&#10;        throw SomeError.illegalArg(&amp;#34;0不能作为除数&amp;#34;)&#10;    }&#10;    return num1 / num2&#10;}&#10;&#10; var value = try divide(20,10)&lt;/code&gt;&lt;/pre&gt;&lt;h2 class="heading-element" id="do-catch-捕捉异常"&gt;&lt;span&gt;do-catch 捕捉异常&lt;/span&gt;&#10; &lt;a href="#do-catch-%e6%8d%95%e6%8d%89%e5%bc%82%e5%b8%b8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;blockquote&gt;&#10;&lt;p&gt;抛出Error后， try下一句直到作用域结束的代码都将停止运行&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>泛型</title><link>https://apolla.cc/posts/%E6%B3%9B%E5%9E%8B/</link><pubDate>Mon, 15 Apr 2024 14:13:34 +0800</pubDate><guid>https://apolla.cc/posts/%E6%B3%9B%E5%9E%8B/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;p&gt;泛型可以将类型参数化，提高代码复用率，减少代码量&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;func swapValues&amp;lt;T&amp;gt;(_ a: inout T, _ b: inout T) {&#10;    (a, b) = (b, a)&#10;}&#10;&#10;enum Score&amp;lt;T&amp;gt; {&#10;    case point(T)&#10;    case grade(String)&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="a-泛型函数"&gt;&lt;span&gt;A. 泛型函数&lt;/span&gt;&#10; &lt;a href="#a-%e6%b3%9b%e5%9e%8b%e5%87%bd%e6%95%b0" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;blockquote&gt;&#10;&lt;p&gt;泛型函数指的是：函数的&lt;code&gt;参数&lt;/code&gt;或&lt;code&gt;返回值&lt;/code&gt;类型使用&lt;code&gt;泛型&lt;/code&gt;，而&lt;code&gt;不是具体的类型&lt;/code&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>运算符重载</title><link>https://apolla.cc/posts/%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD/</link><pubDate>Mon, 15 Apr 2024 14:55:01 +0800</pubDate><guid>https://apolla.cc/posts/%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;p&gt;在C++中, 运算符是可以重载的, Swift也是支持的&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;注：&lt;code&gt;=&lt;/code&gt;和三目运算符&lt;code&gt;a ? b : c&lt;/code&gt;不可重载&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;重载双目运算符&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;class Vector {&#10; var x: Double = 0.0&#10; var y: Double = 0.0&#10; var z: Double = 0.0&#10; &#10; init(x: Double, y: Double, z: Double) {&#10; self.x = x&#10; self.y = y&#10; self.z = z&#10; }&#10; &#10; convenience init() {&#10; self.init(x: 0, y: 0, z: 0) &#10; }&#10; &#10; &#10; &#10; static func &amp;#43;(v1: Vector, v2: Vector) -&amp;gt; Vector {&#10; let vector: Vector = Vector()&#10; vector.x = v1.x &amp;#43; v2.x&#10; vector.y = v1.y &amp;#43; v2.y&#10; vector.z = v1.z &amp;#43; v2.z&#10; return vector&#10; }&#10;} &lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;单目运算符&lt;/strong&gt;&lt;/p&gt;</description></item><item><title>扩展</title><link>https://apolla.cc/posts/%E6%89%A9%E5%B1%95/</link><pubDate>Mon, 15 Apr 2024 15:07:54 +0800</pubDate><guid>https://apolla.cc/posts/%E6%89%A9%E5%B1%95/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;p&gt;&lt;em&gt;扩展_可以给一个现有的类，结构体，枚举，还有协议添加新的功能。它还拥有不需要访问被扩展类型源代码就能完成扩展的能力（即_逆向建模&lt;/em&gt;）。扩展和 Objective-C 的分类很相似。（与 Objective-C 分类不同的是，Swift 扩展是没有名字的。）&lt;/p&gt;&#10;&lt;p&gt;Swift 中的扩展可以：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;添加计算型实例属性和计算型类属性&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;定义实例方法和类方法&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;提供新的构造器&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;定义下标&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;定义和使用新的嵌套类型&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;使已经存在的类型遵循（conform）一个协议&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>函数式编程、协议编程、响应式编程</title><link>https://apolla.cc/posts/%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B%E5%8D%8F%E8%AE%AE%E7%BC%96%E7%A8%8B%E5%93%8D%E5%BA%94%E5%BC%8F%E7%BC%96%E7%A8%8B/</link><pubDate>Mon, 15 Apr 2024 16:31:53 +0800</pubDate><guid>https://apolla.cc/posts/%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B%E5%8D%8F%E8%AE%AE%E7%BC%96%E7%A8%8B%E5%93%8D%E5%BA%94%E5%BC%8F%E7%BC%96%E7%A8%8B/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h3 class="heading-element" id="1函数式编程"&gt;&lt;span&gt;1、函数式编程&lt;/span&gt;&#10; &lt;a href="#1%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;h5 class="heading-element" id="1array-常见方法"&gt;&lt;span&gt;1、Array 常见方法&lt;/span&gt;&#10; &lt;a href="#1array-%e5%b8%b8%e8%a7%81%e6%96%b9%e6%b3%95" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h5&gt;&lt;pre&gt;&lt;code&gt;var arr = [1, 2, 3, 4]&#10;&#10;var arr2 = arr.map { $0 * 2 }&#10;&#10;var arr3 = arr.filter { $0 % 2 == 0 }&#10;&#10;var arr4 = arr.reduce(0) { $0 &amp;#43; $1 }&#10;&#10;var arr5 = arr.reduce(0, &amp;#43;) &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;func double(_ i: Int) -&amp;gt; Int { i * 2 }&#10;var arr = [1, 2, 3, 4]&#10;&#10;print(arr.map(double)) &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;var arr = [1, 2, 3]&#10;&#10;var arr2 = arr.map { Array.init(repeating: $0, count: $0) }&#10;&#10;var arr3 = arr.flatMap { Array.init(repeating: $0, count: $0) } &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;var arr = [&amp;#34;123&amp;#34;, &amp;#34;test&amp;#34;, &amp;#34;jack&amp;#34;, &amp;#34;-30&amp;#34;]&#10;&#10;var arr2 = arr.map { Int($0) }&#10;&#10;var arr3 = arr.compactMap { Int($0) } &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;// 使用reduce实现map、filter的功能&#10;var arr = [1, 2, 3, 4]&#10;// [2, 4, 6, 8]&#10;print(arr.map { $0 * 2 })&#10;print(arr.reduce([]) { $0 &amp;#43; [$1 * 2] })&#10;// [2, 4]&#10;print(arr.filter { $0 % 2 == 0 })&#10;print(arr.reduce([]) { $1 % 2 == 0 ? $0 &amp;#43; [$1] : $0 }) &lt;/code&gt;&lt;/pre&gt;&lt;h5 class="heading-element" id="2lazy-的优化"&gt;&lt;span&gt;2、lazy 的优化&lt;/span&gt;&#10; &lt;a href="#2lazy-%e7%9a%84%e4%bc%98%e5%8c%96" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h5&gt;&lt;pre&gt;&lt;code&gt;let arr = [1, 2, 3]&#10;let result = arr.lazy.map { (i: Int) -&amp;gt; Int in&#10; print(&amp;#34;mapping \(i)&amp;#34;)&#10; return i * 2&#10;}&#10;print(&amp;#34;begin-----&amp;#34;)&#10;print(&amp;#34;mapped&amp;#34;, result[0])&#10;print(&amp;#34;mapped&amp;#34;, result[1])&#10;print(&amp;#34;mapped&amp;#34;, result[2])&#10;print(&amp;#34;end----&amp;#34;)&#10;&#10;begin-----&#10;mapping 1&#10;mapped 2&#10;mapping 2&#10;mapped 4&#10;mapping 3&#10;mapped 6&#10;end---- &lt;/code&gt;&lt;/pre&gt;&lt;h5 class="heading-element" id="3optional-的-map-和-flatmap"&gt;&lt;span&gt;3、Optional 的 map 和 flatMap&lt;/span&gt;&#10; &lt;a href="#3optional-%e7%9a%84-map-%e5%92%8c-flatmap" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h5&gt;&lt;pre&gt;&lt;code&gt;var num1: Int? = 10&#10;&#10;var num2 = num1.map { $0 * 2 }&#10;var num3: Int? = nil&#10;&#10;var num4 = num3.map { $0 * 2 } &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;var num1: Int? = 10&#10;&#10;var num2 = num1.map { Optional.some($0 * 2) }&#10;&#10;var num3 = num1.flatMap { Optional.some($0 * 2) } &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;var num1: Int? = 10&#10;var num2 = (num1 != nil) ? (num1! &amp;#43; 10) : nil&#10;var num3 = num1.map { $0 &amp;#43; 10 } &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;var fmt = DateFormatter()&#10;fmt.dateFormat = &amp;#34;yyyy-MM-dd&amp;#34;&#10;var str: String? = &amp;#34;2011-09-10&amp;#34;&#10;&#10;var date1 = str != nil ? fmt.date(from: str!) : nil&#10;&#10;var date2 = str.flatMap(fmt.date) &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;var score: Int? = 98&#10;&#10;var str1 = score != nil ? &amp;#34;socre is \(score!)&amp;#34; : &amp;#34;No score&amp;#34;&#10;&#10;var str2 = score.map { &amp;#34;score is \($0)&amp;#34; } ?? &amp;#34;No score&amp;#34; &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;struct Person {&#10; var name: String&#10; var age: Int&#10;}&#10;var items = [&#10; Person(name: &amp;#34;jack&amp;#34;, age: 20),&#10; Person(name: &amp;#34;rose&amp;#34;, age: 21),&#10; Person(name: &amp;#34;kate&amp;#34;, age: 22)&#10;]&#10;&#10;func getPerson1(_ name: String) -&amp;gt; Person? {&#10; let index = items.firstIndex { $0.name == name }&#10; return index != nil ? items[index!] : nil&#10;}&#10;&#10;func getPerson2(_ name: String) -&amp;gt; Person? {&#10; return items.firstIndex { $0.name == name }.map { items[$0] }&#10;} &lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;struct Person {&#10; var name: String&#10; var age: Int&#10; init?(_ json: [String : Any]) {&#10; guard let name = json[&amp;#34;name&amp;#34;] as? String, let age = json[&amp;#34;age&amp;#34;] as? Int else {&#10; return nil&#10; }&#10; self.name = name&#10; self.age = age&#10; }&#10;}&#10;var json: Dictionary? = [&amp;#34;name&amp;#34; : &amp;#34;Jack&amp;#34;, &amp;#34;age&amp;#34; : 10]&#10;&#10;var p1 = json != nil ? Person(json!) : nil&#10;&#10;var p2 = json.flatMap(Person.init) &lt;/code&gt;&lt;/pre&gt;&lt;h5 class="heading-element" id="4函数式编程"&gt;&lt;span&gt;4、函数式编程&lt;/span&gt;&#10; &lt;a href="#4%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h5&gt;&lt;p&gt;函数式编程（Funtional Programming，简称FP）是一种编程范式，也就是如何编写程序的方法论。&lt;br&gt;&#10;主要思想：&lt;code&gt;把计算过程尽量分解成一系列可复用函数的调用&lt;/code&gt;。&lt;br&gt;&#10;主要特征：函数是&lt;code&gt;第一等公民&lt;/code&gt;。函数与其他数据类型一样的地位，可以赋值给其他变量，也可以作为函数参数、函数返回值。&lt;/p&gt;</description></item><item><title>访问控制、内存管理、字面量、模式匹配</title><link>https://apolla.cc/posts/%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E5%AD%97%E9%9D%A2%E9%87%8F%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/</link><pubDate>Mon, 15 Apr 2024 16:37:01 +0800</pubDate><guid>https://apolla.cc/posts/%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E5%AD%97%E9%9D%A2%E9%87%8F%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/</guid><category domain="https://apolla.cc/categories/swift/">Swift</category><description>&lt;h3 class="heading-element" id="1访问控制"&gt;&lt;span&gt;1、访问控制&lt;/span&gt;&#10; &lt;a href="#1%e8%ae%bf%e9%97%ae%e6%8e%a7%e5%88%b6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;Swift 提供了5个不同的访问级别：open &amp;gt; public &amp;gt; internal &amp;gt; fileprivate &amp;gt; private&lt;br&gt;&#10;open：允许在定义实体的模块、其他模块中访问，允许其他模块进行继承、重写（open只能用在类、类成员上）。&lt;br&gt;&#10;public：允许在定义实体的模块、其他模块中访问，不允许其他模块进行继承、重写。&lt;br&gt;&#10;internal：只允许在定义实体的模块中访问，不允许在其他模块中访问。绝大部分实体默认都是该级别。&lt;br&gt;&#10;fileprivate：只允许在定义实体的源文件中访问。&lt;br&gt;&#10;private：只允许在定义实体的封闭声明中访问。&lt;/p&gt;</description></item><item><title>葵花宝典</title><link>https://apolla.cc/posts/interview/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://apolla.cc/posts/interview/</guid><description>iOS 面试题合集：基础、原理、UI、架构与高频真题</description></item><item><title>蓝牙</title><link>https://apolla.cc/posts/bluetooth/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://apolla.cc/posts/bluetooth/</guid><description>iOS 蓝牙开发：连接流程、常用 API、Mac 地址与指令处理</description></item><item><title>碎片芝士收藏</title><link>https://apolla.cc/posts/tips/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://apolla.cc/posts/tips/</guid><description>iOS 开发零散技巧与踩坑记录</description></item><item><title>一阅阅读</title><link>https://apolla.cc/posts/reading/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://apolla.cc/posts/reading/</guid><description>「一阅阅读」App 开发过程中的问题与解决方案记录</description></item><item><title>音视频</title><link>https://apolla.cc/posts/media/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://apolla.cc/posts/media/</guid><description>音视频基础：FFmpeg 编译、RGB/YUV、SDL 与 OpenAL</description></item><item><title>直播</title><link>https://apolla.cc/posts/live/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://apolla.cc/posts/live/</guid><description>音视频直播实战：FFmpeg、ijkplayer、视频流与推拉流</description></item><item><title>组件化</title><link>https://apolla.cc/posts/modularization/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://apolla.cc/posts/modularization/</guid><description>iOS 组件化与模块化：CocoaPods 二进制化、subspecs、解耦实践</description></item></channel></rss>