1

I'm trying to run this code in 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)

I copied it from https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations

The error I get:

Playground execution failed:

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

Why do I get this error in the playground? When using wait(for:timeout:) in a normal project, it works.

Display Name
  • 4,502
  • 2
  • 47
  • 63

1 Answers1

0

As noted by 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.

To successfully use the class method in your Playground, prefix it with the class name to disambiguate XCTWaiter.wait(for:timeout:):

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

On the other hand, if one day you want to use the conflicting method from wait.h, which has a Swift signature of public func wait(_: UnsafeMutablePointer<Int32>!) -> pid_t, you may prefix it with the Darwin module name:

Darwin.wait()
Cœur
  • 37,241
  • 25
  • 195
  • 267