简体   繁体   English

Swift 5.5 async let - 错误:表达式为“异步”但未标记“等待”

[英]Swift 5.5 async let - error: expression is 'async' but is not marked with 'await'

WWDC21 introduces Swift 5.5 , with async/await . WWDC21 引入了Swift 5.5 ,带有async/await Following the Explore structured concurrency in Swift and Meet async/await in Swift WWDC21 sessions, I'm trying to use the async let function.在 Swift 中探索结构化并发在 Swift WWDC21 会话中遇到异步/等待之后,我正在尝试使用异步让function。

Here's my Playground code:这是我的游乐场代码:

func doIt() async -> String {
    let t = TimeInterval.random(in: 0.25 ... 2.0)
    Thread.sleep(forTimeInterval: t)
    return String("\(Double.random(in: 0...1000))")
}

async {
    async let a = doIt()
    async let b = doIt()
    async let c = doIt()
    async let d = doIt()
    
    let results = await [a, b, c, d]
    for result in results {
        print("  \(result)")
    }
}

However, for each of the "async let" lines, I get this error:但是,对于每个“async let”行,我都会收到此错误:

error: AsyncLetSwift55WWDC21.playground:12:15: error: expression is 'async' but is not marked with 'await'
    async let a = doIt()
              ^
              await 

Paul Hudson's blog showed this example: Paul Hudson 的博客展示了这个例子: 使用 Swift 异步进行黑客攻击示例

The Exploring structured currency video has this example at about the 8:10 mark:探索结构化货币视频在 8:10 左右有这个例子: 在此处输入图像描述

EDIT: This does seem to be a Playground-specific issue.编辑:这似乎是一个特定于游乐场的问题。 Per the suggestion on the same issue in Apple Developer Forums , running the same code ( ok, I did add sleep(10) to the end of the source file after the async block for macOS, so the app wouldn't terminate before the async calls completed ) as a macOS command-line project gives no errors and produces the proper output.根据Apple Developer Forums中关于同一问题的建议,运行相同的代码(好的,我确实在 macOS 的异步块之后将sleep(10)添加到源文件的末尾,因此应用程序不会在异步之前终止调用完成)作为 macOS 命令行项目没有错误并产生正确的 output。

Is this a bug, or am I just not understanding something?这是一个错误,还是我只是不明白什么?

My advice would be: don't try this in a playground.我的建议是:不要在操场上尝试这个。 Playgrounds aren't ready for this stuff yet.游乐场还没有为这些东西做好准备。 Your code compiles and runs fine in a real project.您的代码在实际项目中编译并运行良好。 Here's an example:这是一个例子:

class ViewController: UIViewController {
    
    func doIt() async -> String {
        let t = TimeInterval.random(in: 0.25 ... 2.0)
        Thread.sleep(forTimeInterval: t)
        return String("\(Double.random(in: 0...1000))")
    }
    func test() {
        async {
            async let a = doIt()
            async let b = doIt()
            async let c = doIt()
            async let d = doIt()
            
            let results = await [a, b, c, d]
            for result in results {
                print("  \(result)")
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        test()
    }
}

Building as a macOS command line project (Xcode: File -> New -> Project, then select "Command Line Tool" from the macOS tab), the code works perfectly.构建为 macOS 命令行项目(Xcode:文件 -> 新建 -> 项目,然后是 macOS 选项卡中的 select “命令行工具”),代码运行良好。 (This was a suggestion from a response in the Apple Developer Forums .) (这是来自Apple Developer Forums回复的建议。)

I've added a single sleep(10) to the end so that the command line tool doesn't exit before the async calls finish:我在末尾添加了一个sleep(10) ,以便在异步调用完成之前命令行工具不会退出:

import Foundation

print("Hello, world!")

func doIt() async -> String {
    let t = TimeInterval.random(in: 0.25 ... 2.0)
    Thread.sleep(forTimeInterval: t)
    return String("\(Double.random(in: 0...1000))")
}

async {
    async let a = doIt()
    async let b = doIt()
    async let c = doIt()
    async let d = doIt()
    
    let results = await [a, b, c, d]
    for result in results {
        print("  \(result)")
    }
}
sleep(10)

This produces the expected sort of console output (Note: the actual values will differ on every run)*:这会产生预期的控制台 output (注意:每次运行的实际值都会有所不同)*:

Hello, World!
  415.407747869283
  574.28639828183
  689.4706625185836
  385.56539085197113
Program ended with exit code: 0

It runs well as macOS command line project, but when I created it as a iOS project,它作为 macOS 命令行项目运行良好,但是当我将它创建为 iOS 项目时,

Those codes:那些代码:

async let a = doIt()
async let b = doIt()
async let c = doIt()
async let d = doIt()

DO NOT runs in parallel.不要并行运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM