Swift playground错误:在调用中额外参数'timeout'

问题描述 投票:1回答:1

我正在尝试在操场上运行此代码:

//: 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)
                                  ^~~~

为什么我在操场上出现这个错误?在正常项目中使用wait(for:timeout :)时,它可以工作。

ios swift xcode xctest swift-playground
1个回答
0
投票

正如Shripada所说:

如果类/结构方法与具有相同名称但不同参数的全局方法之间存在冲突,则会发生此错误。

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

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

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

Darwin.wait()
© www.soinside.com 2019 - 2024. All rights reserved.