<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Swift 5.8 - 标签 - 宋明的博客</title><link>https://apolla.cc/tags/swift-5.8/</link><description>宋明的博客</description><generator>Hugo 0.170.0 &amp; FixIt v1.0.0-rc.5-mwimukpl</generator><language>zh-cn</language><lastBuildDate>Fri, 31 Mar 2023 08:00:00 +0800</lastBuildDate><atom:link href="https://apolla.cc/tags/swift-5.8/index.xml" rel="self" type="application/rss+xml"/><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></channel></rss>