在Swift闭包中隐式使用self

时间:2019-04-30 20:58:39

标签: swift

我有以下测试课:

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)

        }
    }
}

在线:

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

编译器警告我红色错误:

  

在闭包中隐式使用“自我”;使用“自我”。使捕获语义明确化

但是我不使用任何自变量。为什么不编译?

1 个答案:

答案 0 :(得分:2)

由于DogYearsTests继承自XCTestCase,所以期望是类的一种方法,因此应使用self进行调用。