Q:

Implicit use of self in Swift closure

I have following test class:
import XCTest
@testable import DogYears

class DogYearsTests: XCTestCase {

    let calc = Calculator()
    var resData: Data? = nil

    override func setUp() {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testResult(){
        let res1 = calc.evaluate(op: "+", arg1: 2.0, arg2: 9.0)
        let res2 = calc.result
        XCTAssert(res1 == res2)
    }

    func testInfoLoading(){

        let url = "https://raw.githubusercontent.com/FahimF/Test/master/DogYears-Info.rtf"
        HTTPClient.shared.get(url: url) { (data, error) in
            XCTAssertNil(error)
            XCTAssertNotNil(data, "empty data")
            self.resData = data
            let predicate = NSPredicate(format: "resData != nil")
            let exp = expectation(for: predicate, evaluatedWith: nil, handler: nil)

        }
    }
}

On line:
 let exp = expectation(for: predicate, evaluatedWith: nil, handler: nil)

Compiler warn me with red error:

Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit

But I don't use any self variables. Why doesn't it compile?

A:

Since DogYearsTests inherits from XCTestCase, expectation is a method of your class, therefore you should call it with self.

