简体   繁体   English

Swift错误“变量在其初始值内使用”

[英]Swift Error “Variable used within its own initial value”

I am writing my XCUITest's for my app. 我正在为我的应用程序编写XCUITest。 I am declaring the alert in order to use waitForExpectationsWithTimeout to make my test asynchronous....However it is throwing the error Variable used within its own initial value on the declaration of alert on line 5. 我声明警报是为了使用waitForExpectationsWithTimeout使我的测试异步....但是,它在第​​5行的alert声明中抛出了错误Variable used within its own initial value

    let timeout = NSTimeInterval()
    let app = XCUIApplication()

    let exists = NSPredicate(format: "exists == 1")
    let alert = alert.buttons["OK"]


    testCase.addUIInterruptionMonitorWithDescription("Enable Notifications") { (alert) -> Bool in
            alert.buttons["OK"].tap()
        return true
    }

    self.buttons["Enable notifications"].tap()
    testCase.expectationForPredicate(exists, evaluatedWithObject: alert, handler: nil)
    testCase.waitForExpectationsWithTimeout(timeout, handler: nil)
    app.tap()

Can someone tell me why it is throwing this error and what I can do to fix this. 有人可以告诉我为什么会引发此错误,以及我可以采取哪些措施来解决此问题。 Thanks in advance. 提前致谢。

It is because in your line no. 这是因为在您的行号。 5, you have written 5,你写了

let alert = alert.buttons["OK"]

alert was never declared before this line, so you cannot write this. 在此行之前从未声明过alert,因此您不能编写此代码。

For example, take this case, 举例来说,

let a = a+5

Now compiler will throw the same error as it does not know the value of 'a' as it was not declared before. 现在,编译器将抛出相同的错误,因为它不知道之前未声明的'a'值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM