美文网首页
Swift5.7(下)

Swift5.7(下)

作者: 我想哟 | 来源:发表于2022-06-20 18:31 被阅读0次

1、新的时间表示法

SE-0329 为 Swift 引入了一种新的标准化方式来引用时间和持续时间。它可以拆解为三个主要部分:

  • 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()
}

2、顶级代码的并发

SE-0343 升级了 Swift 对顶级代码的支持——想想 macOS 命令行工具项目中的 main.swift —— 以便它支持开箱即用的并发。这个变化看起来微不足道,但为了支持它需要相当多的工作。

实践上,这个变化意味着我们可以将这样的代码直接写入 main.swift 文件:

let url = URL(string: "https://hws.dev/readings.json")!
let (data, _) = try await URLSession.shared.data(from: url)
let readings = try JSONDecoder().decode([Double].self, from: data)
print("Found \(readings.count) temperature readings")

相关文章

  • Swift5.7(下)

    1、新的时间表示法 SE-0329[https://github.com/apple/swift-evolutio...

  • Swift5.7(上)

    [https://github.com/apple/swift-evolution]apple[https://g...

  • 下下҈下҈下҈下҈下҈下҈雪҈啦҈ 🧣🧣🧣

    更多的排版新样式|排版技巧|涨粉技巧|运营经验, 请关注135编辑器,或者直接关注135编辑器公众号(ID:edi...

  • 207(下)评语下

    李可欣(10): 这学期对你来说一定收获满满吧!课内学习你依然保持你一贯的认真的态度,课外也体验也也一样是宝贵的财...

  • 下洲•下伊

    记忆中,从下洲到下伊的路似乎很漫长。从下洲到下伊,爷爷走了无数次。从下伊到下洲,奶奶一走就是一辈子。 从汤溪镇往中...

  • 下猫下狗

    你的眼睛是红色系的 你的嘴唇有渐变的效果 你的睫毛浓密又纤长 你穿一条露肩的黑色连衣裙 仿佛要去参加一场晚宴 你只...

  • 下吧,下吧

    酝酿了许久的阴沉天气,终于在白昼刚扯开幕布时,细细雪花悠然飘洒起来。 还在睡梦中,手机闹铃就响个不停。每一个闹钟身...

  • 测试下测试下

    输入内容

  • 北下!北下!

    按照剧情设计,我应该是在寒冬凛冽离开的时节,才准备搭上这趟十余年前送我来的列车,但那时可能因为家中事务,我...

  • 《战国史》下·下

    文/精读猫 在政治方面,他们则主张需静无为。 不要总想着改变什么。 你越想把事情扭曲好的方向,他可能就变得越糟糕。...

网友评论

      本文标题:Swift5.7(下)

      本文链接:https://www.haomeiwen.com/subject/ssodvrtx.html