Swift playground error: extra argument 'timeout' in call

Swift playground error: extra argument 'timeout' in call

我正在尝试 运行 playground 中的这段代码:

//: Playground - noun: a place where people can play

import UIKit
import XCTest

// Create an expectation for a background download task.
let expectation = XCTestExpectation(description: "Download apple.com home page")

// Create a URL for a web page to be downloaded.
let url = URL(string: "https://apple.com")!

// Create a background task to download the web page.
let dataTask = URLSession.shared.dataTask(with: url) { (data, _, _) in

    // Make sure we downloaded some data.
    XCTAssertNotNil(data, "No data was downloaded.")

    // Fulfill the expectation to indicate that the background task has finished successfully.
    expectation.fulfill()

}

// Start the download task.
dataTask.resume()

// Wait until the expectation is fulfilled, with a timeout of 10 seconds.
wait(for: [expectation], timeout: 10.0)

我从https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations

复制过来的

我得到的错误:

Playground execution failed:

error: MyPlayground2.playground:21:35: error: extra argument 'timeout' in call
wait(for: [expectation], timeout: 10.0)
                                  ^~~~

为什么我在 playground 中会出现这个错误?在普通项目中使用 wait(for:timeout:) 时,它有效。

Shripada 所述:

This error will ensue, if there is a conflict between a class/struct method, and a global method with same name but different arguments.

要在您的 Playground 中成功使用 class 方法,请在其前面加上 class 名称以消除歧义 XCTWaiter.wait(for:timeout:):

XCTWaiter.wait(for: [expectation], timeout: 10.0)

另一方面,如果有一天您想要使用来自 wait.h 的冲突方法,它具有 public func wait(_: UnsafeMutablePointer<Int32>!) -> pid_t 的 Swift 签名,您可以在其前面加上 Darwin模块名称:

Darwin.wait()