Swift 5.7 新特性

Swift 5.7 内置于 Xcode 14,重点增加了如下几个与实际开发相关的新特性。

简化的if let/guard let语法

let name: String? = "zhangsan"
print(name) // Optional("zhangsan")

// Swift5.7之前
// if let
if let name = name {
    print(name) // zhangsan
}

// Swift5.7之后
// if let
if let name {
    print(name) // zhangsan
}

func method(name: String?, age: Int?) {
    // Swift5.7之前
    // guard let
    guard let name = name else { return }

    // Swift5.7之后
    // guard let
    guard let age else { return }
}

增强的闭包类型推断

let array: [String] = ["1", "2", "3", "4", "5"]

// Swift5.7之前
let newArray1 = array.map { str -> Int in
    Int(str) ?? 0
}
print(newArray1) // [1, 2, 3, 4, 5]

// Swift5.7之后
let newArray2 = array.map { str in
    Int(str) ?? 0
}
print(newArray2) // [1, 2, 3, 4, 5]

正则表达式

  • 引入了新的正则类型Regex
  • 增加了通过/.../创建正则表达式的功能。
  • 增加了许多基于正则表达式的 String 处理方法。
let message = "This is a regex test case 123456789"

do {
    // 字符串中搜索regex
    let regex = try Regex("[a-z] regex")
    // 范围
    print(message.ranges(of: regex))
    // 替换
    print(message.replacing(regex, with: "正则表达式"))
} catch {
    print("Failed to create Regex")
}

// /.../创建正则表达式,查找数字并替换
print(message.replacing(/(\d+)/, with: "一二三四五六七八九"))

函数参数与返回类型支持不透明结果类型

import SwiftUI

// 参数与返回值支持不透明结果类型
// some后面是一个协议
func getSomeView(username: some StringProtocol, image: String) -> (some View, some View) {
    return (Text(username), Image(systemName: image))
}

struct ContentView: View {
    let someView: (some View, some View) = getSomeView(username: "zhangsan", image: "heart")

    var body: some View {
        VStack(spacing: 10) {
            someView.0
                .font(.title)

            someView.1
                .foregroundStyle(.tint)
        }
    }
}

新的时间表示法

  • Clock:表示一种测量时间流逝的方式。又分为 2 种。
    • ContinuousClock:系统处于睡眠状态时也记录时间。
    • SuspendingClock:系统处于睡眠状态时不会记录时间。
  • Instant:表示一个精准的瞬间时间。
  • Duration:表示 2 个Instant之间的时间间隔。
// 当前时间 + 3秒
ContinuousClock.Instant.now + Duration.seconds(3)
// 当前时间 + 50毫秒
ContinuousClock.Instant.now + Duration.microseconds(50)

// 应用于Concurrency
try await Task.sleep(until: .now + .seconds(1), clock: .suspending)
try await Task.sleep(until: .now + .seconds(1), tolerance: .seconds(0.5), clock: .continuous)

// 异步函数
func doSomeAsyncWork() async throws {
    try await Task.sleep(until: .now + .seconds(1), clock: .continuous)
}

// SuspendingClock
let suspendingClock = SuspendingClock()
// 测量
let elapsedOne = try await suspendingClock.measure {
    try await doSomeAsyncWork()
}

// ContinuousClock
let continuousClock = ContinuousClock()
// 测量
let elapsedTwo = try await continuousClock.measure {
    try await doSomeAsyncWork()
}

顶级代码支持Concurrency

Swift 5.7 之前 Concurrency 的代码必须放入函数或者 Task 中,不能出现在顶级代码中,Swift 5.7 之后没有这个限制。因此在 macOS 的命令行项目中可以书写以下代码。

let url = URL(string: "https://www.baidu.com")!
let (data, _) = try await URLSession.shared.data(from: url)
let decoder = JSONDecoder()
let result = String(decoding: data, as: UTF8.self)
print(result)

@MainActor警告修复

在 Swift 5.6 中 @MainActor 产生的警告在 Swift 5.7 中得到了修复,不再出现警告。

import SwiftUI

@MainActor
class ViewModel: ObservableObject {
}

struct ContentView: View {
    // 不再产生警告
    @StateObject private var viewModel = ViewModel()

    var body: some View {
        Text("Hello, world!")
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 对于MySQL的历史,相信很多人早已耳熟能详,这里就不要赘述。下面仅从产品特性的角度梳理其发展过程中的里程碑事件。...
    零一间阅读 5,691评论 1 21
  • MySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,...
    秦泽超阅读 12,712评论 0 1
  • Raw strings 相关资料 SE-0200原始字符串,说白了就是所见即所得,输入的字符串长什么样子,输出的就...
    GUANGGG阅读 9,987评论 0 51
  • 对于很多刚接触java语言的初学者来说,要了解一门语言,最好的方式就是要能从基础的版本进行了解,升级的过程,以及升...
    timothyue1阅读 2,755评论 0 1
  • 概览 Swift 5 发布了,这是一个重要里程碑。 此版本终于迎来了 ABI 稳定,因此 Swift 运行时现在可...
    _森宇_阅读 9,225评论 0 7

友情链接更多精彩内容