swift - 在 Swift 闭包中隐式使用 self

标签 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)

编译器警告我红色错误:

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

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

最佳答案

由于 DogYearsTests 继承自 XCTestCase,expectation 是你类的一个方法,因此你应该用 self 调用它。

关于swift - 在 Swift 闭包中隐式使用 self,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55928578/

相关文章:

ios - 将项目复制到新笔记本电脑后出现 Apple Mach-O 链接器错误

ios - 协议(protocol)扩展和子类

swift 最后(:where) element in an array

arrays - Swift 3 比较数组索引

json - 将下面提到的 Json 字符串转换为字典

ios - TableView 单元格不显示更新的数据

ios - 获取 indexPath.row 的字符串值

ios - 阻止 UITextField 编辑但检测点击

ios - 快速在 iPhone 应用程序的事件指示器中旋转的自定义图像

swift - 当我选择不同的 UITextField 时如何更改 UIPickerViewData?