简体   繁体   中英

Swift Error “Variable used within its own initial value”

I am writing my XCUITest's for my app. 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.

    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

let alert = alert.buttons["OK"]

alert was never declared before this line, so you cannot write this.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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