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

时间:2018-06-07 08:51:57

标签: ios swift xcode xctest swift-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)
                                  ^~~~

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

1 个答案:

答案 0 :(得分:0)

Shripada所述:

  

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

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

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

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

Darwin.wait()